Search code examples
objective-cuitableviewuisearchdisplaycontroller

Reloading searchResultsTableView


I have, in my main tableView, some custom cells (cells with an imageView, basically).

The imageView is set only if a value on my plist is false. Then, when it's true, the imageView is nil.

Basically when the user enters the detailView, the value is set to YES and the cell.imageView is nil.

And it's okay, it works

I'm using a searchDisplayController, when i search for something that has a cell.imageView, going into the detailView and then coming back to the searchResultsTable, the cell has still the image, while it shouldn't, but the main tableView has the correct cell (so, with no image).

I thought that it could depend on searchResultsTableView, but i'm not sure. I tried with

[self.searchDisplayController.searchResultsTableView reloadData]; 

with no effect.

How could i reload the searchResultsTableView so that it shows the right cells, those with the image and those that don't have the image anymore?

Any help appreciated!

EDIT

This is my cellForRowAtIndexPath method:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    NSArray *rows;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        rows = filteredList; //for search
    } else {
        NSDictionary *section = [localSortedTips objectAtIndex:indexPath.section];
        rows = [section objectForKey:@"Rows"];
    }

    NSDictionary *item = [rows objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"name"];  
    if ([[item valueForKey:@"isRead"] boolValue] == NO) {
        cell.imageView.image = [UIImage imageNamed:@"unread.png"];
    } else {
        cell.imageView.image = nil;
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;

    return cell;
}

Solution

  • If I understood you right, then you can have a workaround but searching again with the same search string:

    if (self.searchDisplayController.active) {
        self.searchDisplayController.searchBar.text = self.searchDisplayController.searchBar.text;
    }
    

    put it in viewWillAppear: or viewDidAppear: which will be called each time the view is shown up (eg. you go back from the detail view to your search view). And reloading the data in this place would be nice too, to get the right data (for example if you marked the cell as read like in your sample code)

    Just [self.tableView reloadData]; and not the searchResultsTableView (it will be automatically use the updated data after the new search)