The accepted method seems dirty, it just creates a bunch of new cells that are stored along with the bad ones. Here are a few solutions depending on your situation:
1. Firstly, for the situation described in the question, you should not reset your cells and create new views in each cycle. You need to mark your view and then return it when from the cell, when you get the reuse cell:
- (UITableViewCell*) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath { UITableViewCell *cell = [tableView dequeueResuableCellWithIdentifier:SOME_ID]; if(!cell) { cell = [[UITableViewCell alloc] init]; UIView *myView = [[UIView alloc] init]; cell.backgroundView = myView; [myView setTag:5];
2. ... or even better, why not just use the backgrouncolor for the cells and update it without creating a view.
3. The surefire way to really clear old cached cells is to simply recreate the UITableView object.
4. In most cases, you do not need to destroy these cells, just keep track of your items and update them after receiving the reused cell. You can mark all your elements, save a link to them in an array, find them in the hierarchy of views ... Im Of course, this is a bunch of other ways.
5. heres one liner to immediately clean all cells, although it is not the best practice to mess with the insides of objects like this as they might change in future versions:
[(NSMutableDictionary*)[tableview valueForKey:@"_reusableTableCells" ] removeAllObjects];
source share