iphonecocoa-touchuitableview

Force UITableView to dump all reusable cells


I have a UITableView where I have the backgroud color set via

UIView *myView = [[UIView alloc] init];
if ((indexPath.row % 2) == 0)
    myView.backgroundColor = [UIColor greenColor];
else
    myView.backgroundColor = [UIColor whiteColor];

cell.backgroundView = myView;
[myView release];

The problem I find is that when I edit a table (via setEditing:YES...) some cells of the same color invariable are next to each other. How do I force UITableView to fully redraw. reloadData is not doing a great job.

Is there are deep-cleaning redraw?


Solution

  • I had this issue before so I'll share with you how I solved it:

    You can use a boolean flag (say it's called needsRefresh) to control the behavior of cell creation in -cellForRowAtIndexPath:

    An example:

    - (UITableViewCell*) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
        UITableViewCell *cell = [tableView dequeueResuableCellWithIdentifier:SOME_ID];
        if(!cell || needsRefresh) {
           cell = [[[UITableViewCell alloc] init....] autorelease];
        }
        //.....
        return cell;
    }
    

    So, when you need a hard reload, set the needsRefresh flag to YES. Simple as a pimple.