I’ve got a UIView that I’m adding to a cell’s content view in a particular section (Section 1 specifically), as shown below:
[cell.contentView addSubview:self.overallCommentViewContainer];
When I quickly scroll up/down – the UIView appears in Section 0 – even though I never added the UIView to any of the cell’s in Section 0.
Here’s a detailed look at my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"CustomCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
}
// Configure the cell.
switch(indexPath.section) {
case 0:
// other code
break;
case 1:
// add the overall comment view container to the cell
NLog(@"adding the overallCommentViewContainer");
[cell.contentView addSubview:self.overallCommentViewContainer];
NLog(@"creating the row at: Section %d, Row %d", indexPath.section, indexPath.row);
break;
}
return cell;
}
if the UITableView has cells ready for reuse, its dequeueReusableCellWithIdentifier method will happily return a cell for section 0 that was originally used in section 1! I’d recommend something like this for you to keep them separate:
The key is to use a different identifier string for each type of cell you’re using that is not interchangeable with other cells in the table.