The tableView displayed below contains label. They are added as subView for each cell.
When I run my program for the first time, the cells that appear on the screen have the correct text. But when I scroll the table, the text in the labels begin to get mixed up. From what I understand the label of one cell is overlaid on top of another when the cell is created. This has been shown in the latter two screenshots. After each scroll, the different cells are overlaid on top of the other. (So there is no fixed pattern.) I tried to understand the problem by observing which cell which were going out of the screen and the ones which were entering. I tried to correlate this information with the labels on the cells, but I am not able to understand.
I request you to kindly help me understand the cause for this discrepancy.
Thanks you!

The relevant section of my code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(@"s = %d, r = %d", indexPath.section, indexPath.row);
if (indexPath.section == 0)
{
switch(indexPath.row)
{
case 0:
[cell addSubview:classA];
break;
case 1:
[cell addSubview:classB];
break;
case 2:
[cell addSubview:classC];
break;
case 3:
[cell addSubview:classD];
break;
case 4:
[cell addSubview:classE];
break;
default:
break;
}
}
if(indexPath.section == 1)
{
switch(indexPath.row)
{
case 0:
[cell addSubview:classF];
break;
case 1:
[cell addSubview:classG];
break;
case 2:
[cell addSubview:classH];
break;
default:
break;
}
}
if(indexPath.section == 2)
{
switch (indexPath.row)
{
case 0:
[cell addSubview:classI];
break;
case 1:
[cell addSubview:classJ];
break;
case 2:
[cell addSubview:classK];
break;
default:
break;
}
}
return cell;
}
The reason is, you are using
dequeueReusableCellWithIdentifierto reuse the cells. So when you are scrolling, the cells which are going out of visible area are getting reused for the current visible cells.Since you have already added labels like
classBetc.. in these cells and not removed after that, and after that when you are adding labels such asclassIetc.. on the same reused cells, it is getting drawn on top of this label. Since these labels have background color set as clear color, it will show the label which was drawn behind it also and hence giving an overlapping feeling.As per the screenshot, all the cells are looking identical and you can definitely reuse the cells with
dequeueReusableCellWithIdentifier. So why dont you change your logic of creating the label outsidecellForRowAtIndexPathand instead just use it ascell.TextLabel.text = @"Class B";etc.. in the corresponding if conditions inside this method?Your code will look like,
Update: Here is a not-so-good fix for
dequeueReusableCellWithIdentifierreuse in case all cells are static. But this is not a recommended approach.Update2: So if you are facing issues with above try this. Use my first approach and do the following.
In
removeAllTextFieldsFromCell:method use the R.A’s suggestion.For eg:-
Or just use,