In my tableView, in some cells i have added an imageView as subview of cell contentView. On scrolling tableView up and down these images duplicating on other cells also. But this problem doesn't occur always. Please suggest a solution.. I am using the following code.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (friendsArray.count != 0)
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
NSString *object;
if (friendsArray.count == 0)
{
if ([cell.contentView viewWithTag:indexPath.row])
{
for (UIView *subview in [cell.contentView subviews])
{
[subview removeFromSuperview];
}
}
object = @"No friends added to the list";
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
else
{
object = [friendsArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
if (![cell.contentView viewWithTag:indexPath.row])
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
See Question&Answers more detail:os