举两个全局数组
NSMutableArray *muary_Interest_Main;
NSMutableArray *muary_Interest_Sub;
在viewDidLoad方法
muary_Interest_Main = [[NSMutableArray alloc]initWithObjects:@"Cricket",@"Dancing",@"Painting",@"Swiming",@"guitar",@"movie",@"boxing",@"drum",@"hockey",@"chessing",@"gamming",
@"hunting",@"killing",@"shoping",@"jamm"@"zooming", nil];
muary_Interest_Sub = [[NSMutableArray alloc]init];
tbl_Search = [[UITableView alloc] initWithFrame:
CGRectMake(4, 200, 320, 120) style:UITableViewStylePlain];
tbl_Search.delegate = self;
tbl_Search.dataSource = self;
tbl_Search.scrollEnabled = YES;
[self.tbl_Search registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"];
[self.view addSubview:self.tbl_Search];
[tbl_Search setHidden:TRUE];
现在写在文本框代表一个下面的代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%d",textField.tag);
int_TextFieldTag = textField.tag;
[self searchText:textField replacementString:@"Begin"];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
tbl_Search.hidden = TRUE;
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
tbl_Search.hidden = TRUE;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self searchText:textField replacementString:string];
return YES;
}
写方法搜索文本
-(void) searchText:(UITextField *)textField replacementString:(NSString *)string
{
if(int_TextFieldTag == 1)
{
tbl_Search.frame = CGRectMake(4, 200, 320, 120);
}
else if(int_TextFieldTag == 2)
{
tbl_Search.frame = CGRectMake(4, 248, 320, 120);
}
else if(int_TextFieldTag == 3)
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}
else if(int_TextFieldTag == 4)
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}
else
{
tbl_Search.frame = CGRectMake(4, 268, 320, 120);
}
NSString *str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
if([string isEqualToString:@"Begin"])
str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
else if([string isEqualToString:@""])
str_Search_String = [str_Search_String substringToIndex:[str_Search_String length] - 1];
else
str_Search_String=[str_Search_String stringByAppendingString:string];
muary_Interest_Sub=[[NSMutableArray alloc] init];
if(str_Search_String.length>0)
{
NSInteger counter = 0;
for(NSString *name in muary_Interest_Main)
{
NSRange r = [name rangeOfString:str_Search_String options:NSCaseInsensitiveSearch];
if(r.length>0)
{
[muary_Interest_Sub addObject:name];
}
counter++;
}
if (muary_Interest_Sub.count > 0)
{
NSLog(@"%@",muary_Interest_Sub);
tbl_Search.hidden = FALSE;
[self.tbl_Search reloadData];
}
else
{
tbl_Search.hidden = TRUE;
}
}
else
{
[tbl_Search setHidden:TRUE];
}
}
泰伯维委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [muary_Interest_Sub count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
;
}
cell.textLabel.text = [muary_Interest_Sub objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
if(int_TextFieldTag == 1)
{
txt1.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else if(int_TextFieldTag == 2)
{
txt2.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else if(int_TextFieldTag == 3)
{
txt3.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else if(int_TextFieldTag == 4)
{
txt4.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
else
{
txt5.text=[muary_Interest_Sub objectAtIndex:indexPath.row];
}
}
这也适用于文本框的退格。 试试这个。希望这会适合您的要求。
您是否尝试过任何回答四位,或只是张贴问题? – Maul 2014-09-30 07:19:54
您输入**“coff”或“coff”? – abhishekkharwar 2014-09-30 07:54:03
它的coff只,而我发布的问题,我让它大胆这就是为什么它显示出一些错误 – 2014-09-30 08:03:56