Search code examples
iosuisearchbaris-empty

Searchbar works correctly but doesn't show any results


I've added a search bar to my project and it works quite well. You can search for object (of a table view) and the relations also work. But the search display doesn't show the result names.

For example: I have no object which begins with "x" >>> So no result (That's correct):

But one object starts with "b", but the name isn't displayed although I can click on it and it shows me the next view (Relation) correctly:

Maybe this is caused due to my code:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

if (tableView == self.searchDisplayController.searchResultsTableView) {

    Car *search = [searchResults objectAtIndex:indexPath.row];
    UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
    carNameLabel.text = search.name;

I don't know why this doesn't work and this seems to be very strange to me. It would be great if someone can help me.

Update: Full method

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

if (tableView == self.searchDisplayController.searchResultsTableView) {

     Car *search = [searchResults objectAtIndex:indexPath.row];
    UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
    carNameLabel.text = search.name;

} else {
    Car *car = [cars objectAtIndex:indexPath.row];
    UIImageView *carImageView = (UIImageView *)[cell viewWithTag:100];
    carImageView.image = [UIImage imageNamed:car.thumbnail];

    UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
    carNameLabel.text = car.name;

    UILabel *carSpeedLabel = (UILabel *)[cell viewWithTag:102];
    carSpeedLabel.text = car.carSpeed;

}

return cell;

}


Solution

  • You're attempting to fetch the UILabels that you're setting by tag values, but you're using a stock UITableViewCell. Try the following:

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        Car *search = [searchResults objectAtIndex:indexPath.row];
        cell.textLabel.text = search.name;
    } else {
        Car *car = [cars objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:car.thumbnail];
    
        cell.textLabel.text = car.name;
        cell.detailTextLabel.text = car.carSpeed;
    }
    

    If you don't want to use the standard UIViews that are built into a normal UITableViewCell, you'll have to create and add your custom views (and set their tag property) in the block where you create new cells.