Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchDisplayController Not Displaying Results in searchResultsTableView

I have a UISearchDisplayController properly connected to the header of a custom UITableView in IB. However, when I search for anything the searchResultsTableView only displays "No Results", and I cannot seem to find where the code is incorrect.

searchResults Property

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

    return _searchResults;
}

Table View 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 Bar Delegate 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];
        }
    }
}
like image 418
sethfri Avatar asked Feb 19 '23 20:02

sethfri


1 Answers

Just try this 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 on:

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

This way you deque a Cell from your own tableView and not from the one created by UISearchDisplayController.

like image 142
David H. Avatar answered May 07 '23 19:05

David H.