0

我有一个表格视图并像这样向其中添加项目。

 NSDictionary *memberInfo = [self.currentChannel infoForMemberWithID:memberID];

    memberinfo=memberInfo;

  cell.textLabel.text = [NSString stringWithFormat:@"%@",[memberinfo objectForKey:@"name"]];
  cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[memberinfo objectForKey:@"status"]];


    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];  

  return cell;

`它工作正常。现在我想在上面添加一个搜索栏。我需要根据匹配的字符串加载表格视图。但我正在加载这样的表格视图。我知道如何在数组中搜索。我是使用

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText.i want to load the table view according to the match of the searchstring ,Can anybody know this?
4

2 回答 2

0

您可以在 SearchBar 委托中使用 NSPredicate 来过滤您的主数组(成员或您命名的任何内容)并将结果捕获到另一个全局声明的数组中。现在用这个过滤的数组重新加载你的表。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [_mArrFilteredList removeAllObjects]; //global array which will contain filtered results
    NSString* searchStr = [NSString stringWithFormat:@"*%@*",_srbActivity.text];

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K == %@",@"name", searchStr];  //will filter according to "name" key in your dictionary.
    [_mArrFilteredList addObjectsFromArray:[_mArrList filteredArrayUsingPredicate:predicate]]; //_mArrList is your main array which has all the dictionaries.
    [yourTableView reloadData];
}

现在在您的 tableview 数据源方法中,使用过滤数组 (_mArrFilteredList) 填充表。

于 2012-10-16T10:33:48.037 回答
0

对于任何搜索实现,我们都需要两个列表,一个是原始的,另一个是过滤的。我们通常会使用过滤列表来填充表格。

在您的情况下,您可以将字典作为原始列表。对于过滤列表,您可以NSArray根据要求拥有一个值或键。

在搜索功能中过滤数组列表,重新加载表格。从cellForRow数组键中获取对象或从数组值中获取键 - 稍后该键的对象。

于 2012-10-16T11:06:22.677 回答