How to remove a subview from a cell's content view?
NickName:Zhen Ask DateTime:2011-09-07T21:37:46

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

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 something or if method 2 is the only way to go

Copyright Notice:Content Author:「Zhen」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7334791/how-to-remove-a-subview-from-a-cells-content-view

Answers
omz 2011-09-07T13:42:05

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:\n\n//...\nwebView.tag = 123;\n[cell.contentView addSubview:webView];\n//...\n[[cell.contentView viewWithTag:123] removeFromSuperview];\n\n\nIn practice it would of course be better to use a named constant as the tag.",


Vijay-Apple-Dev.blogspot.com 2011-09-07T13:42:34

//creating\n\n// cell.bounds will fill up the cell with your webview\nUIWebView *webview = [[UIWebView alloc]initWithFrame:cell.bounds];\n\nwebview.tag = 11;\n\n[cell.contentView addSubview:webview]; \n\n //removing\n\n\n[[cell.contentView viewWithTag:11] removeFromSuperview];\n",


Rob Napier 2011-09-07T13:39:42

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.",


More about “How to remove a subview from a cell's content view?” related questions

Remove subview from UITableView cell

I have a table, and based on a simple boolean, I add a subview containing an image to the right side of the cell using this code in cellForRowAtIndexPath: if(myArray[indexPath.row]==YES){ int ...

Show Detail

Remove subview of tableview cell with tag

Im trying to remove a subview from tableview cells which i added like subView.tag = 300; [cell.contentView addSubView:subView]; with this method [cell.contentView viewWithTag:300] performSelector:@

Show Detail

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

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 contentvie...

Show Detail

Remove a subview from a UITableViewCell's content view

How do I remove a Subview from a UITableViewCell's content view? For instance, I have added the following subview to my cell's content view. UIButton *b = etc. [cell.contentView addSubview:b]; N...

Show Detail

When a subview is removed, remove from superview

I am trying to do something very similar to How to tell when a subView is removed a UIView I add a view (A) as a subview, this in turn creates a subview (B) for itself. What it want: When A's sub...

Show Detail

How to remove subview from Superview?

I have taken one subView of type UIView on the top of UIViewController. I want to remove it and again load it after clicking on button. But I am unable to remove it. I have used [subView

Show Detail

How to remove specific subview?

I've added subview (ViewController) to my ViewController: Location *location = [[Location alloc] initWithNibName:@"Location" bundle:[NSBundle mainBundle]]; [self.subView addSubview:location.view];...

Show Detail

Remove subview from view

I want to know how can I remove my HUD subview from the main view + (SLHUD *)Mostrar:(UIView *)view{ SLHUD *hudView = [[SLHUD alloc] initWithFrame:view.bounds]; // Creates an instance of the obje...

Show Detail

When to remove subview from UITableViewCell?

I have a UITableView with searchBar and searchDisplayController. What I wanted to do was to show a button when no results were found. So user could search server's database. I have a NSMutableArrayto

Show Detail

How to remove Dynamic UITextField from Subview

How to remove the array of UITextField from Scroll Subview, initially when I call API I get some Array Value based on Count I have created Dynamic UITextFiled in My View, But I do know how to remov...

Show Detail