iphone - 如何以编程方式添加 UISearchDisplay Controller ?

标签 iphone ios uitableview uisearchdisplaycontroller

我一直在寻找这一天,但没有找到......

有人能给我指点教程/告诉我应该做什么吗? 该文档并没有真正的帮助...... 我的 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/

相关文章:

ios - UITextView在IOS中消耗大量内存

ios - 使用 navigationBarHidden 作为 getter 属性不会出错

ios - UITableView - UITableViewCell.Style .subtitle - 如何使 UIImageView 圆形? - swift 5

iphone - 对于图像元数据,什么标签允许最大字符限制?

iphone - 二维坐标系中的等距投影

IOS Swift 使用 Facebook Login 而不是 FBSDKLoginKit AppDelegate.swift 代码

ios - 我如何与 Metal 沟通我正在避免 GPU 和 CPU 之间的数据冲突

ios - 使用标题 View 自定义导航栏

ios - 在 tableview 中显示来自 json 文件的图像

ios - 带有 TableView 的 UIPageViewController,位于 UITableView 中