在我的 tableView 中,在某些单元格中,我添加了一个 imageView 作为单元格 contentView 的子视图。在上下滚动 tableView 时,这些图像也会复制到其他单元格上。但这个问题并不总是发生。请提出解决方案..我正在使用以下代码。
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];
}
}
}