开发者

How to remove a subview from a cell's content view?

开发者 https://www.devze.com 2023-04-03 10:44 出处:网络
I have added a UIWebview to the cell\'s content view UIWebView *webview = [UIWebView alloc]init]; [cell.contentView addSubview:webview];

I have added a UIWebview to the cell's content view

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

Now I intend to remove 'webview' from the cell's contentview.

Can I do the following?

Method 1:

[webview removeFromSuperview];

Or do I really need to loop through all the subviews of the contentview before removing it?

Method 2:

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

I tried method 1 and it didn't seem to work, am not sure if I am missing some开发者_如何学JAVAthing or if method 2 is the only way to go


Method 1 should work, but are you sure that the web view you're removing is really the same as the one you added? A simple way to remove a given subview from a view is to assign a tag:

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

In practice it would of course be better to use a named constant as the tag.


    //creating

// cell.bounds will fill up the cell with your webview
UIWebView *webview = [[UIWebView alloc]initWithFrame:cell.bounds];

webview.tag = 11;

[cell.contentView addSubview:webview];  

    //removing


[[cell.contentView viewWithTag:11] removeFromSuperview];


You can just use removeFromSuperview. What do you mean by "didn't seem to work?" The most likely problem is that webview is nil at the point that you called removeFromSuperview (or is pointing to some other view). Make sure it's pointing to the view you mean.


I believe that your webview is nil when actually trying it to remove from the superview. Have you tried reloading table data after removing it from the content view? In which methods are you doing view manipulation? Datasource/delegate or custom IBActions?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号