I have some different cells in my tableView, each with different subviews. Each time a cell dissapear and reapear, the subview is addad on top of the old view, and also is added to other cells. What is the correct way to add subviews to cells without using custom cells?
Thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"StatisticsCell";
StatisticsCell *cell = (StatisticsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"StatisticsCell" owner:nil options:nil];
for (id currentObject in topLevelObject)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (StatisticsCell *)currentObject;
break;
}
}
}
if (indexPath.section == 0 && indexPath.row == 0)
{
//Add a subview here
[cell addsubview ....
}
else if (indexPath.section == 0 && indexPath.row == 1)
{
//Add a subview here
[cell addsubview ....
}
etc....
Whenever you scroll cell for row method is called, so when your cell be visible, it will add subview to the cell. Place a check that is view added already, make an ivar that is bool, and set it true when you add view, and false when you remove. Like this: