gpt4 book ai didi

objective-c - 滚动时复制 UITableViewCell contentView 的 subview (UIButton 和 UIImageView)

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:04 25 4
gpt4 key购买 nike

在我的 tableView 中,在某些单元格中,我添加了一个 imageView 作为单元格 contentView 的 subview 。在上下滚动 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];
}
}
}

最佳答案

问题已经解决了。我在创建新 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)];
}

关于objective-c - 滚动时复制 UITableViewCell contentView 的 subview (UIButton 和 UIImageView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12934364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com