[英]How to implement searchbar if the tableview is loaded from a dictionary?
我有一个表格视图,并像这样向其中添加项目。
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;
``它工作正常。现在我想在上面添加一个搜索栏。我需要根据匹配的字符串加载tableview。但是我这样加载tableview。我知道如何在数组中搜索。使用
- (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?
您可以在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)填充表。
对于任何搜索实现,我们都需要两个列表,一个是原始列表,另一个是经过过滤的列表。 我们通常将使用过滤列表来填充表格。
就您而言,您可以将字典作为原始列表。 对于过滤列表,您可以根据需要使用NSArray
值或键。
在搜索功能中过滤数组列表,重新加载表。 在cellForRow
,从数组键中获取对象,或者从数组值中获取键,之后的对象为该键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.