gpt4 book ai didi

ios - 从 UITableView 单元格中删除 subview

转载 作者:行者123 更新时间:2023-11-28 22:09:54 25 4
gpt4 key购买 nike

我有一个表格,基于一个简单的 bool 值,我使用 cellForRowAtIndexPath 中的以下代码将包含图像的 subview 添加到单元格的右侧:

if(myArray[indexPath.row]==YES){
int xpos=self.mainTableView.frame.size.width+self.mainTableView.frame.origin.x-24;
int ypos=(cell.frame.size.height/2)-(18/2);
UIImageView *imv=[[UIImageView alloc]initWithFrame:CGRectMake(xpos,ypos, 18, 18)];
imv.image=[UIImage imageNamed:@"transition-30"];
[cell.contentView addSubview:imv];
}

如果“myArray[x]”的值为NO,如何删除之前添加的 subview ?

我试过使用:[cell.contentView removeFromSuperview] 但这会删除单元格的全部内容 - 包括文本。

最佳答案

你必须在 imv 上调用 removeFromSuperview --> [imv removeFromSuperview];

要获取先前添加的 imv,您可以将 imvtag 属性设置为常量,并在下次调用时获取它使用 viewWithTag(参见 here)

所以在添加 subview 之前:

imv.tag = 1;
[cell.contentView addSubview:imv];

删除 subview 时,您首先必须使用

触发所需单元格的重新加载
reloadRowsAtIndexPaths:(NSArray *)indexPaths 
withRowAnimation:(UITableViewRowAnimation)animation;

如果 myArray[indexPath.row] == NO

,然后在 cellForRowAtIndexPath 中执行以下操作
UIImageView *imv = (UIImageView*)[cell.contentView viewWithTag:1];
[imv removeFromSuperview];

使用 tag 属性是不好的做法,因此请考虑创建您自己的 UITableViewCell
参见 here有关如何执行此操作的说明。

关于ios - 从 UITableView 单元格中删除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174741/

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