Reloading searchResultsTableView

柔情痞子 提交于 2019-12-08 08:53:54

问题


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;
}

回答1:


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)




回答2:


It sounds as if perhaps your cells are being recycled and not reset properly. UITableView uses view recycling, so it's important that if you do something such as set an image you make sure it is explicitly set even when their isn't an image to display.

If you share your cellsForRowAtIndexPath code you might be able to get some more help.



来源:https://stackoverflow.com/questions/11352403/reloading-searchresultstableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!