UISearchDisplayController does not display results in searchResultsTableView

I have a UISearchDisplayController correctly connected to a custom UITableView header in IB. However, when I search for something, searchResultsTableView only displays β€œNo Results”, and I cannot find where the code is incorrect.

searchResults Property

 - (NSMutableArray *)searchResults { if (!_searchResults) { _searchResults = [[NSMutableArray alloc] initWithCapacity:self.listingPreviews.count]; } return _searchResults; } 

Table data source methods

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSInteger numberOfRows = 0; if (tableView == self.tableView) { numberOfRows = self.listingPreviews.count; } else { numberOfRows = self.searchResults.count; } return numberOfRows; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Listing"; ListingPreviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[ListingPreviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell. if (tableView == self.tableView) { cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row]; } else { NSLog([[self.searchResults objectAtIndex:indexPath.row] title]); cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row]; } return cell; } 

Search Panel Delegation Method

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSInteger searchTextLength = searchText.length; for (ListingPreview *listingPreview in self.listingPreviews) { if ((listingPreview.title.length >= searchTextLength) && ([listingPreview.title rangeOfString:searchText].location != NSNotFound)) { [self.searchResults addObject:listingPreview]; } } } 
+4
source share
2 answers

Try instead:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Listing"; ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell. if (tableView == self.tableView) { cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row]; } else { NSLog([[self.searchResults objectAtIndex:indexPath.row] title]); cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row]; } return cell; 

}

Please note that the change was applied to:

ListingPreviewTableViewCell * cell = [ self.tableView dequeueReusableCellWithIdentifier: CellIdentifier];

Thus, you decake the cell from your own View table, and not from the created UISearchDisplayController.

+8
source

Nothing in your code seems to force searchDisplayController to reload its searchResultTableView.

The standard approach is to set UISearcDisplayController's delegate (note the protocol - UISearchDisplayDelegate ) and implement – searchDisplayController:shouldReloadTableForSearchString:

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { // perform your search here and update self.searchResults NSInteger searchStringLength = searchString.length; for (ListingPreview *listingPreview in self.listingPreviews) { if ((listingPreview.title.length >= searchStringLength) && ([listingPreview.title rangeOfString:searchString].location != NSNotFound)) { [self.searchResults addObject:listingPreview]; } } // returning YES will force searchResultController to reload search results table view return YES; } 
0
source

All Articles