【问题标题】:Search Bar and Search Display Controller on the ViewControllerViewController 上的搜索栏和搜索显示控制器
【发布时间】:2014-03-12 22:17:22
【问题描述】:
我有一个视图控制器,上面有两个按钮,我希望当用户按下按钮时 - searchBar 出现,他可以写一些文本,在搜索显示上他可以看到搜索结果.这个问题让我抓狂,在我只使用 tableViews 之前,在数据源方法中检查
if (tableView == self.searchDisplayController.searchResultsTableView)
{
// searching result
}
else
{
//just tableView
}
我如何理解 searchDisplayController 和 tableView - 是分开的 vc,并且对于搜索我不需要 tableView。但我无法实现它,我试图在这个门户网站上搜索这个问题,但找不到我需要的东西。如果有关于这个问题的教程或示例代码?提前致谢
【问题讨论】:
标签:
ios
objective-c
uitableview
uisearchbar
【解决方案1】:
我认为最好的理解方式是你的 ViewController 中有两个 UITableViews。一个是你的普通UITableView,另一个是searchDisplayController.searchResultsTableView。当您处于搜索模式时,普通的UITableView 被停用,而另一个被激活。由于两者具有相同的 Datasource 和 Delegate(在您的 VC 中),因此您需要检查所有 Delegate- 和 Datasource-methods 如果调用 TableView 是其中之一并返回正确的结果。例如,在numberOfRowsInSection 中,您可能需要返回 100 作为完整数组的计数,但只返回 20 作为过滤数组的计数。
像这样:
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (aTableView == self.tableView) {
return [self.contentArray count];
} else {
return [self.searchResultArray count];
}
}
其他方法相同。