我一直在寻找这一天,但没有找到......
有人能给我指点教程/告诉我应该做什么吗? 该文档并没有真正的帮助...... 我的 UITableViewController 类需要这个(没有 xib!)
非常感谢!
最佳答案
首先做Gray UISearchBar w/matching scope bar programmatically
然后,由于 tableview 和 searchtableview 都使用相同的数据源,因此您必须在 tableview 委托(delegate)方法中插入 if 语句,如下所示:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResutsTableView) {
tableViewData = searchResultsData objectatindex...; //array with filtered data
} else {
tableViewData = defaultData objectatindex...; //array with unfiltered data
}
}
对行数委托(delegate)方法执行相同的操作:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return searchResultsData.count;
} else {
return defaultData.count;
}
}
然后在搜索文本更改或按下搜索按钮后(查看 UISearchBar 委托(delegate)方法),重新加载数据以显示。
关于iphone - 如何以编程方式添加 UISearchDisplay Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6994808/