【问题标题】:AutoComplete textfield with static array data in IOSIOS中带有静态数组数据的自动完成文本字段
【发布时间】: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


【解决方案1】:

要使用搜索字符串过滤您的城市,您可以尝试使用以下代码

-(void)getAutoCompletePlaces:(NSString *)searchToken
{
  //Store original city array at somewhere to reset filtering

  if(searchToken.length >0)
  {
    NSMutableString *arg = [NSMutableString string];
    [arg appendString:@"\\s*\\b"];
    [arg appendString:searchToken];
    [arg appendString:@"\\b\\s*"];
    //arg above is for exact matching string which comes to start, mid or end
    NSPredicate *matchPredicate = [NSPredicate predicateWithFormat:@"SELF matches[c] %@", arg]; 
    //Format can be 'SELF CONTAINS[c]' than 'SELF MATCHES[c]' both work with different needs

    //For 'CONTAINS' format no need to create arg
    NSPredicate *containPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", arg]; 

    //Get new filtered array from old city array
    NSArray *newArray = [city filteredArrayUsingPredicate:containPredicate]; //can be matchPredicate

    //use newArray to reload table
    city = newArray

    //Reload
    self.autotable.hidden=NO;
    [_autotable reloadData];
  }
}

你可以根据自己的需要修改谓词格式

【讨论】:

  • 它没有显示字母表,它只是显示一个数组。 @Abhishek
  • 写谓词格式“SELF CONTAINS[c] %@”,它会工作
  • @Oneeb 更新的答案尝试使用 matchPredicate 和 containsPradicate 两者,以前没有写过 'city = newArray' 所以可能不起作用
  • 让我试试。 @Abhishek
  • 现在在表格视图中没有显示任何内容。 @Abhishek
【解决方案2】:

我建议filtering a UITableView with a search bar 而不是使用文本字段。根据您尝试实现的目标,查看教程并相应地调整您的代码。

更新:如果您想继续使用您的文本字段,只需在为文本字段设置通知后过滤数组,然后更新它。我还没有测试过,但请尝试一下。

[self.cityName addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

-(void)textFieldDidChange :(UITextField *)theTextField{
    if(self.cityName.text.length>0) {
        [self getAutoCompletePlaces:self.cityName.text];
    }
}

-(void)getAutoCompletePlaces:(NSString *)searchToken {
    if(city.count > 0) {
        city = [city filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchToken]]; //Filters the array by the key word or character.
        self.autotable.hidden=NO;
        [_autotable reloadData];
    }
}

【讨论】:

  • 我想要它在一个文本字段中。 @本尼男孩
  • 刚刚更新了它,不完全确定它是否能解决您的问题,因为它会过滤“城市”数组,这意味着它将变得未排序。 @Oneeb
猜你喜欢
  • 1970-01-01
  • 2021-06-21
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多