gpt4 book ai didi

objective-c - 如何从单元格的内容 View 中删除 subview ?

转载 作者:技术小花猫 更新时间:2023-10-29 11:02:28 24 4
gpt4 key购买 nike

我已经在单元格的内容 View 中添加了一个 UIWebview

UIWebView *webview = [UIWebView alloc]init];
[cell.contentView addSubview:webview];

现在我打算从单元格的内容 View 中删除“webview”。

我可以执行以下操作吗?

方法一:

[webview removeFromSuperview];

或者我真的需要在删除之前遍历内容 View 的所有 subview 吗?

方法二:

for (UIView *subview in [self.contentView subviews]) 
{
if ([subview isKindOfClass:[UIWebView class]])
{
[subview removeFromSuperview];
}
}

我尝试了方法 1 但它似乎没有用,我不确定我是否遗漏了什么或者方法 2 是否是唯一的方法

最佳答案

方法 1 应该可行,但您确定要删除的 WebView 与添加的 WebView 真的一样吗?从 View 中删除给定 subview 的一种简单方法是分配一个标签:

//...
webView.tag = 123;
[cell.contentView addSubview:webView];
//...
[[cell.contentView viewWithTag:123] removeFromSuperview];

在实践中,使用命名常量作为标记当然会更好。

关于objective-c - 如何从单元格的内容 View 中删除 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334791/

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