Question

I have got an application which has a UITableView containing two sections. Now I wanted to add a UISearchBar to allow searching. And the searching really works pretty fine and selects the correct cells. When searching you can see that there are cells you can select, but the labels are blank. Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"qrCodeCell";
    QRTableViewCell *cell = (QRTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    if (indexPath.section == 0) {
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd, yyyy HH:mm"];
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            //NSLog(@"%@",[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]);
            [cell.dataLabel setText:[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]];
            //NSLog(@"%@",cell.dataLabel.text);

            NSDate *timeStamp = [[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        } else {
            [cell.dataLabel setText:[[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        }
    } else if (indexPath.section == 1) {
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd, yyyy HH:mm"];
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            [cell.dataLabel setText:[[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        } else {
            [cell.dataLabel setText:[[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        }
    }
    return cell;
}

This is pretty much exactly the same issue as in these questions, but the problem could not be resolved: UISearchBar with UITableView containing custom cells => blank cells and It doesn't show the contents of the cells in a UITableView filtered by a UISearchDisplayController

I would really appreciate if someone can help me!

EDIT:

Here is some more code from my TableViewController:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"data contains[c] %@", searchText];
    self.searchResultsQR = [self.qrcodes filteredArrayUsingPredicate:resultPredicate];
    self.searchResultsScanned = [self.scannedCodes filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
Was it helpful?

Solution

Your cell is not registered properly. This means that your code will run:

cell = [[QRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

which will not create all of your subviews or connect any of the outlets.

You need to find out why your cell isn't registered properly. If you created it in a storyboard then it should be contained in the table view where it will be used (which should automatically register the cell). If you created it in a standalone XIB file then you need to load the NIB and register it explicitly when the table view is loaded.

You have 2 table views, the cell is registered with one but not the other - this is the way prototype cells work.

Your best options:

-Remove the prototype cell, create an XIB and register the NIB with each table view explicitly

-Not to use a search controller (so you only have 1 table view, manage the search bar yourself)

These are a few references which you can look forward to.

This is a tutorial for implementing search delgates with storyboard

Also found a discussion on SO which might give you some direction of where you are going wrong

Search Bar in UITableView doesn't display text in Cell label

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top