Search code examples
iphoneuitableviewcocos2d-iphoneuisearchbaruisearchdisplaycontroller

cellForRowAtIndexPath not called from UISearchDisplayController delegates


EDIT: Thanks for the answers below. I couldn't get the UISearchDisplayController to reload the table, so I just ended up calling the filter methods when the search text changed and changing the array used to populate the table.


I've added a UISearchDisplayController to a UITableView programmatically as follows (this is using Cocos2d). cellForRowAtIndexPath is originally called when the view loads, but cellForRowAtIndexPath is not called when the search delegates return YES.

(self is a UIViewController)

    table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 80.0f, 480.0f, 260.0f)];
    table.delegate = self;
    table.dataSource = self;
    [[[CCDirector sharedDirector] openGLView] addSubview:table];

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    [searchBar sizeToFit];
    searchBar.delegate = self;
    searchBar.placeholder = @"Search";
    table.tableHeaderView = searchBar;

    UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDC.delegate = self;
    searchDC.searchResultsDataSource = self;
    searchDC.searchResultsDelegate = self;

//        [searchBar release];
//        [searchDC release];

When a character is entered into the search bar, the following delegates are called, numberOfRowsInSection is called, but cellForRowAtIndexPath is never called.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    // Do filtering and add to the search array
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[[controller.searchBar scopeButtonTitles] objectAtIndex:[controller.searchBar selectedScopeButtonIndex]]];

    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[controller.searchBar text] scope:[[controller.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    return YES;
}

Solution

  • Just Check Following things:

    1)tableview delegate & datasource are properly set.

    2)In coming data in not empty.

    3)check method numberOfRowsInSection: returning proper value.