UISearchDisplayController not correctly displaying custom cells

11,734

Solution 1

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

did not work because table view cells are registered to a specific table view. This will not work for your search results controller table view. You did find this out yourself and switched to:

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

which is the right thing to do.

Also, designing your custom cell in storyboard will not really work for your search results controller because you are not able to design cells for search table view, only for the main table view.

Yes, you can register that class for your search table view, as you did here,

[self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];

but that will not have any of the stuff you designed in your custom cell in storyboard. You would have to create all programmatically.

Solution 2

I got the same exception when I tried to search and some how this fixes it.

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }else{
       cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     
    }

Solution 3

My summary for UITableView with Search Bar and Search Display using same custom cell designed in storyboard protype:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifierAsYouDefinedInStoryboard";

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

    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        /* searchResultsTableView code here */
    } else {
        /* Base tableView table code here */
    }

    /* more cell code here */

    return cell;
}


and then add this line for searchResultsTableView to match your custom cell height:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight];

     /* more of your viewDidLoad code */
}

Solution 4

If you are using a UITableView in a UIViewController and you want to reuse a Cell Identifier you created in your StoryBoard for your searchDisplayController, try this:

StoryBoard > UIViewController > Reference Outlets > link tableView to your UIViewController's .h file and call it something like tableView so you should have something like this:

@property (strong, nonatomic) IBOutlet UITableView *tableView;

So rather than doing it like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}

do this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}
Share:
11,734
terry lewis
Author by

terry lewis

iOS developer, C.S student, building both my own and client apps.

Updated on June 05, 2022

Comments

  • terry lewis
    terry lewis about 2 years

    So I have a tableView that has sections and rows, and it uses a custom cell class. The custom cell has an image view and a few labels. The table view works fine, and the search works, except the search does not display any of the labels that are in my custom cell class, only the imageView with the correct image. I am quite confused as to why this is, especially since the image is still displayed, but not the labels. Here is some code.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //TODO: problem with search view controller not displaying labels for the cell, needs fixing
    JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if(cell == nil) {
        cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    JSBook *book = nil;
    //uses the appropriate array to pull the data from if a search has been performed
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
    }
    else {
        book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
    }
    
    FFMetaData *data = [self.ff metaDataForObj:book];
    cell.titleLabel.text = book.title;
    cell.priceLabel.text = [NSString stringWithFormat:@"$%@", book.price];
    cell.authorLabel.text = book.author;
    cell.descriptionLabel.text = book.description;
    
    cell.dateLabel.text = [self.formatter stringFromDate:data.createdAt];
    if(book.thumbnail == nil) {
        cell.imageView.image = [UIImage imageNamed:@"messages.png"];
        [self setCellImage:cell withBook:book atIndex:indexPath withTableView:tableView];
    }
    else {
        cell.imageView.image = [UIImage imageWithData:book.thumbnail];
    }
    
    return cell;
    

    }

    Before this problem, I had only one section in the tableView, and everything worked perfectly. Now that I have multiple sections and rows the search is broken as I described. Any ideas? Also, for [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; I used to have [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; But now if I use that I get a weird exception when I try to search:

    NSInternalInconsistencyException', reason: 'request for rect at invalid index path ( 2 indexes [1, 1])'

    So that is confusing me also. Thanks for the help!