ios 如何在xib的tableview中实现SearchBar和搜索显示控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18145209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement SearchBar and search display controller in tableview in xib
提问by
I have mutable array with dictionaries.I`am displaying that array in table view.
我有带有字典的可变数组。我正在表视图中显示该数组。
Now i want to implement search and display controller to table view. How?
现在我想在表格视图中实现搜索和显示控制器。如何?
Any suggestions or code..
任何建议或代码..
Here my array i`am displaying "name" key in uitableview as alphabetically order.
在这里,我的数组在 uitableview 中按字母顺序显示“名称”键。
[
{
"name": "Fish",
"description": "sdhshs",
"colorCode": null,
},
{
"name": "fry",
"description": "sdhshs",
"colorCode": null,
},
{
"name": "curry",
"description": "sdhshs",
"colorCode": null,
}
],
采纳答案by slysid
Here is a sample code
这是一个示例代码
NSMutableArray *filteredResult; // this holds filtered data source
NSMutableArray *tableData; //this holds actual data source
-(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
[filteredResult removeAllObjects]; // clearing filter array
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.restaurantName contains[c] %@",text]; // Creating filter condition
filteredResult = [NSMutableArray arrayWithArray:[tableData filteredArrayUsingPredicate:filterPredicate]]; // filtering result
}
Delegate Methods
委托方法
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterForSearchText:searchString scope:[[[[self searchDisplayController] searchBar] scopeButtonTitles] objectAtIndex:[[[self searchDisplayController] searchBar] selectedScopeButtonIndex] ]];
return YES;
}
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
In NSPredicate condition "@"SELF.restaurantName contains[c] %@",text " restaurantName is a property name which needs to filtered against. If you have only NSString in your datasource array, you can use like @"SELF contains[c] %@",text
在 NSPredicate 条件 "@"SELF.restaurantName contains[c] %@",text " restaurantName 是需要过滤的属性名称。如果你的数据源数组中只有 NSString,你可以使用像 @"SELF contains[c] %@",text
Once the filter is done, then you need to implement your tableview delegate accordingly. Something like this
过滤器完成后,您需要相应地实现您的 tableview 委托。像这样的东西
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == [[self searchDisplayController] searchResultsTableView])
{
return [filteredResult count];
}
else
{
return [tableData count];
}
}
compare the tableview whether it is filtered tableview or original tableview and set the delegate and datasource for tableview accordingly.Please note, searchDisplayController is available property for UIViewcontroller and we can just use it to display filtered result.
比较tableview是过滤tableview还是原始tableview,并相应地设置tableview的delegate和datasource。请注意,searchDisplayController是UIViewcontroller的可用属性,我们可以使用它来显示过滤结果。
For above code to work, you need to use "Search Bar and Search Display" object if you are using it in a XIB or storyboard
要使上述代码正常工作,如果您在 XIB 或故事板中使用它,则需要使用“搜索栏和搜索显示”对象
回答by Venk
Refer the following sample codes,
参考以下示例代码,
http://www.appcoda.com/how-to-add-search-bar-uitableview/
http://www.appcoda.com/how-to-add-search-bar-uitableview/
These examples may give you a better idea
这些例子可能会给你一个更好的主意