我一直在寻找这一天,但我找不到....
谁能给我指个教程/告诉我应该怎么做?文档并不是很有帮助……我需要这个为我的UITableViewController类(没有xib!)
非常感谢!
首先以编程方式对UISearchBar进行匹配
然后,由于tableview和searchtableview都使用相同的数据源,你必须在tableview委托方法中插入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
}
}
对行数委托方法执行相同操作:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return searchResultsData.count;
} else {
return defaultData.count;
}
}
然后在搜索文本更改或搜索按钮被按下之后(查看UISearchBar委托方法),重新加载数据以显示。