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]; } } }
source share