简体   繁体   中英

Problem while removing UItableviewcell subviews in iphone development

i am using following code for removing subviews from UItableviewcell to clear out previous subviews before adding new ones

if ([cell.contentView subviews]) {

   for (UIView *subview in [cell.contentView subviews]) {

    [subview removeFromSuperview];
    }
}

problem here is that one of the subviews is an UIImageView object and because of which after removing these imageview (subviews) my application crashes. I cannot even keep it as it is as multiple Imageviews stacking makes application heavier and application eventually throws memorylevel warnings and crashes.

Thanks in advance

You should NOT remove all subviews like that. You don't know where they came from. Instead (if you must remove the subviews) keep an NSMutableArray of all the subviews you add. Then, when you want to remove them, just iterate through that list and call removeFromSuperview , then clear your array.

When you don't create a view, you shouldn't remove its subviews. I've been burned by that before. You don't know which ones you added, and which ones were added before. You can easily remove things like scroll bars, disclosure indicators, and other important things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM