1

我正在制作一个演示天气应用程序,其中我正在使用 UISearchBar 以便用户输入城市名称,但我不知道如何显示建议。

例如,如果用户输入“lon”,则应该有城市名称建议以“lon”开头,如伦敦等。

4

2 回答 2

1

UISearchBar有自己的委托协议UISearchBarDelegate,方法——

-(BOOL)searchBar:(UISearchBar *)searchBar
shouldChangeTextInRange:(NSRange)range
  replacementText:(NSString *)text

可以在编辑时做一些额外的操作,在这里你可以NSPredicate看看插入的文本是否有城市BEGINSWITHCONTAINS输入的文本。

于 2015-04-01T07:15:12.277 回答
0

你可以这样做,

  1. 创建要显示的地方的数组(列表)...在数组中添加对象!

    NSArray *list;

    NSArray *listFiles;

2.过滤内容..

-(void)filter :(NSString *) match1


    listFiles = [[NSMutableDictionary alloc] init];

    NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"FullName BEGINSWITH[c] %@", match1];

    listFiles = [[ContactDect filteredArrayUsingPredicate:sPredicate ] mutableCopy];
    f_name_array=[[NSMutableArray alloc]init];

    for (NSDictionary *d in listFiles) {

        [self.f_name_array addObject:[d objectForKey:@"FullName"]];

    }
    list=f_name_array;
    NSLog(@"%@",list);



}
  1. 然后在 numberOfRowsInTableview 中计算列表:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [listFiles count]; }

  1. cellForRow ::::

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"abccell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%@" ,[listFiles objectAtIndex:indexPath.row]]; return cell; }

编辑 textFeild 时调用 Filter 方法!

[self filter:textfeild1.text];

我希望它会帮助你......

于 2015-04-01T09:07:07.423 回答