I’m having a very common problem that I don’t know how to fix for myself, even after reading a bunch of similar posts, since making my cells non-reusable is not an option (the app slows down immensely).
The Problem
When I have 6 or more cells (maximum of 16), the contents of the first 5 cells begin appearing in all of the other cells when I scroll to them.
Though I am using reusable cells, why are the contents of my cells not being refreshed with new data that’s stored in my NSMutableArrays?
The Code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WorldClockTableViewCell *worldClockCell = (WorldClockTableViewCell*)[tableView dequeueReusableCellWithIdentifier:kCellID];
if(worldClockCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WorldClockTableViewCell" owner:self options:nil];
worldClockCell = [topLevelObjects objectAtIndex:0];
/*
Set Scrolling to OFF by default.
User can scroll when table is in edit mode.
*/
[worldClockCell.scrollView setScrollEnabled:NO];
// Set Digital Clock in timeLabel
[self setDigitalClockInLabel:worldClockCell inRow:indexPath.row];
// Set Analog Clock Carousel in ScrollView in every cell.
[self setAnalogClocksInScrollView:worldClockCell inRow:indexPath.row];
// Scroll to User's Preferred Clock (if this is the first launch of WorldClockTableViewController)
if([[shouldAutoScrollArray objectAtIndex:indexPath.row] boolValue] == YES) {
[self scrollToUsersPreferredClock:worldClockCell atRow:indexPath.row];
[shouldAutoScrollArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
}
// Add City Name to cityLabel in every cell
[worldClockCell buildFormattedCityName:[model.timeZoneCityNames objectAtIndex:indexPath.row]];
// Make sure new clocks created while in 'edit' mode don't scroll out of the cell
worldClockCell.scrollView.frame = worldClockCell.scrollViewFrame;
}
return worldClockCell;
}
You’re doing all your cell configuration only if it created a new cell, not if it dequeued one. Pull everything out of
except
unless you really only need to do it once, when the cell is created (and not every time it’s reconfigured to display new data).