Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchDisplayController not correctly displaying custom cells

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!

like image 389
terry lewis Avatar asked Apr 09 '13 03:04

terry lewis


1 Answers

[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.

like image 133
svena Avatar answered Sep 17 '22 12:09

svena