【发布时间】:2017-09-12 12:20:45
【问题描述】:
我有一个城市名称的静态数组。我在它下面有一个文本字段和一个表格视图。当我输入城市名称时,它会在表格视图中正确显示我的数组,但当我输入任何字母时它就不起作用。
例如 - 如果我输入 K,它应该在表格视图中显示以 K 开头的城市名称。
我的代码是,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [city count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[city objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.textLabel.text);
self.cityName.text=[city objectAtIndex:indexPath.row];
self.autotable.hidden=YES;
}
这是一种在文本字段上更改的方法,
- (IBAction)editonChanged:(id)sender {
if(self.cityName.text.length>3)
{
[self getAutoCompletePlaces:self.cityName.text];
}
}
-(void)getAutoCompletePlaces:(NSString *)searchToken
{
if(city.count >0)
{
self.autotable.hidden=NO;
[_autotable reloadData];
}
}
【问题讨论】:
-
你能显示一些额外的代码 getAutoCompletePlaces
-
它是我使用的完整代码。 @Anbu.Karthik
-
这是什么
If I enter K it should show city name starting with K,但你称这条线if(self.cityName.text.length>3)那么它是如何出现在第一个字符上的 -
@Oneeb 你应该在重新加载之前在 getAutoCompletePlace 中过滤数组
-
我该如何使用它们。 @Abhishek
标签: ios objective-c arrays autocomplete uitextfield