Creating a UISearchDisplayController programmatically

后端 未结 1 1142
醉梦人生
醉梦人生 2020-12-16 01:39

I\'m trying to create a UISearchDisplayController programmatically. I have a method which should set up my search controller, but when I call it, nothing happen

相关标签:
1条回答
  • 2020-12-16 02:37

    Check out the example code in: [https://github.com/JayMarshal/GrabCasts.com-iPhone-Client/blob/master/CoreDataTableViewController.m][1]

    Repo here: https://github.com/JayMarshal/Grabcasts

    It is an expanded version of the coredatatableviewcontroller of the stanford iOS courses.

    Relevant snippet of that code follows:

    - (void)createSearchBar {
    if (self.searchKey.length) {
        if (self.tableView && !self.tableView.tableHeaderView) {
            UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease];
            self.searchDisplayController 
                   = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                       contentsController:self];
            self.searchDisplayController.searchResultsDelegate = self;
            self.searchDisplayController.searchResultsDataSource = self;
            self.searchDisplayController.delegate = self;
            searchBar.frame = CGRectMake(0, 0, 0, 38);
            self.tableView.tableHeaderView = searchBar;
        }
    } else {
        self.tableView.tableHeaderView = nil;
    }
    

    Basically it attaches the UISearchDisplayController to self (which must be a tableviewcontroller) as a side effect of the initialization. So setting:

    self.searchDisplayController.searchResultsDelegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    

    Instead of

    myCon.searchResultsDataSource = self;
    myCon.searchResultsDelegate = self;
    

    Might do the trick. In debugging, check whether myCon and self.searchDisplayController are pointing to the same object?

    Updated: there seems to be a bug in the SDC property of the TVC that it is not retained in the runloop. Filed as: http://openradar.appspot.com/10254897 also mentioned on SO, see UIViewController does not retain its programmatically-created UISearchDisplayController

    0 讨论(0)
提交回复
热议问题