【问题标题】:Why method updateSearchResultsForSearchController is not called?为什么不调用方法 updateSearchResultsForSearchController?
【发布时间】:2015-06-10 07:49:30
【问题描述】:

我有一个 UICollectionViewController 来维护一个食谱集合。

class RecipesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource  {

另一个 UICollectionViewController 用于维护搜索结果,也像这样实现 UISearchResultsUpdating:

class SearchResultsController: UICollectionViewController, UISearchResultsUpdating,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { ...

我需要搜索集合,所以我在 RecipesViewController 中有一个成员

var searchController: UISearchController!

也是这样发起的:

let resultsController = SearchResultsController()
resultsController.recipes = recipes
resultsController.filteredResults = recipes

searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController

我将来自 searchController 的 UISearchBar 放入 RecipesViewController 的标题中。

我的问题是当文本输入到 searchBar 时 SearchResultsController 没有更新。甚至没有调用方法 updateSearchResultsForSearchController。

【问题讨论】:

    标签: swift uisearchcontroller


    【解决方案1】:
    @interface YourViewController_CollectionResults () <UISearchResultsUpdating>
    
    @end
    
    @implementation YourViewController_CollectionResults
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        // The collection view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
        UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"CollectionSearchResultsNavController"];
    
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    
        //It's really import that you class implement <UISearchResultsUpdating> 
        self.searchController.searchResultsUpdater = self;
    
        self.tableView.tableHeaderView = self.searchController.searchBar;
    
        self.definesPresentationContext = YES;
    
    }
    

    你在这个 github 上有一个例子

    Sample-UISearchController

    我希望这个例子能解决你的问题。

    【讨论】:

    • 这样好多了。
    【解决方案2】:

    这是搜索控制器的惰性实例化:

        self.searchController = ({
            // Setup One: This setup present the results in the current view.
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.hidesNavigationBarDuringPresentation = false
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.searchBarStyle = .Minimal
            controller.searchBar.sizeToFit()
            self.tableView.tableHeaderView = controller.searchBar
            return controller
        })()
    

    希望对你有所帮助!

    【讨论】:

    • 谢谢,但我需要一个在 UICollectionViewController 中使用搜索控制器的示例。我有 2 个集合,第一个用于整个集合,另一个用于管理结果,也符合 UISearchResultsUpdating。在两个集合之间拆分搜索时,我似乎错过了一些东西。
    • 我也尝试使用你的第一个建议,UISearchControllerDelegate 没有成功。
    • 尝试将 self.tableview 更改为 self.collectionview(不确定是否存在但最糟糕的尝试)
    • 我使用 UICollectionElementKindSectionHeader 将搜索栏作为标题放在 RecipesViewController 中。
    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 2018-03-26
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多