在我的tableView中,在一些单元格中,我添加了一个imageView作为单元格contentView的子视图。在滚动表上查看上下这些图像也复制在其他单元格上。但这个问题并不总是发生。请提出解决方案..我正在使用以下代码。
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];
}
}
}
答案 0 :(得分:8)
问题现在解决了。我在创建新tableViewCell的else案例中使用了以下行。
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
我编辑了我的代码,如下所示:
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else
{
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
答案 1 :(得分:0)
请尝试以下代码..
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];
}
}
}
}
答案 2 :(得分:0)
正如Romit Mewada建议你可以使用它,但每次你有alloc&添加新的UIImageView
。
而不是你可以将nil发送给UIImageView
的图像。
以这种方式实施。
if (cell == nil)
{
//get your cell or alloc & initialize
// create and add imageView
}
UIImageView* imageView = [cell.contentView viewWithTag:indexPath.row];
imageView.image = nil;
//do your other task, you can use the same imageView to add other image for other row.