简体   繁体   English

删除UIScrollView子视图帮助

[英]Removing UIScrollView subviews assistance

I'd like some assistance to properly remove subviews from a scrollview, to reduce memory usage. 我想要一些帮助,以从滚动视图中正确删除子视图,以减少内存使用量。 Every time the user scrolls, I ask to grab page, page -1 and page+1 to get the views loaded in the scrollview. 每次用户滚动时,我要求获取页面-1和page + 1,以将视图加载到scrollview中。

Now my issue is that if there's ten views side by side, when you reach the tenth view, all the previous 9 views are still subviews in the scrollview and in memory. 现在我的问题是,如果有十个视图并排显示,那么当您到达第十个视图时,所有先前的9个视图在滚动视图和内存中仍然是子视图。 I'd like to reverse load these views as well. 我也想反向加载这些视图。 Here's what I have so far: 这是我到目前为止的内容:

- (void)loadScrollViewWithPage:(int)page {
    if (page < 0) return;
    if (page >= [self favouritesCount]) return;
    // replace the placeholder if necessary
    MyObject *myObj = (MyObj *)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:page inSection:0]];
    MyView *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {
        controller = [[MyView alloc] initWithObject:myObj];
        controller.delegate = self;
        [viewControllers replaceObjectAtIndex:page withObject:controller];
    }
    // add the controller's view to the scroll view
    if (nil == controller.superview) {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.frame = frame;
        [scrollView addSubview:controller];
    }
}

Now I have tried something like: 现在我已经尝试过类似的东西:

if (page > 2) {
    [(UIView *)[[scrollView subviews] objectAtIndex:(page - 2)] removeFromSuperview];
}

But that just crashes it. 但这只会使其崩溃。 It's probably a simple solution, so help is appreciated :) 这可能是一个简单的解决方案,所以可以提供帮助:)

Did you increment the pageCount? 您增加了pageCount吗?

You can try with this as well: 您也可以尝试以下方法:

MyView *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
   [controller removeFromSuperView];
}

Some notes for naming convention: MyView *controller does not make sense. 命名约定的一些注意事项:MyView * controller没有意义。 If MyView is subclass of UIView, you should only name if MyView *view 如果MyView是UIView的子类,则仅应在MyView * view时命名

I think if I were you, I would just add "loadData" and "releaseData" methods on my views, and call those when they're coming in and out of view. 我想如果我是您,我只会在视图中添加“ loadData”和“ releaseData”方法,并在它们进入和离开视图时调用它们。 Sure, you'll still have the view classes loaded, but in those methods you can get rid of (and re-load) the more heavyweight sub-objects that those views contain. 当然,您仍然会加载视图类,但是在这些方法中,您可以摆脱(并重新加载)那些视图包含的重量更重的子对象。

Good luck, 祝好运,
Blake. 布莱克。

I think your problem is in the solution you posted: 我认为您的问题出在您发布的解决方案中:

[(UIView *)[[scrollView subviews] objectAtIndex:(page - 2)] removeFromSuperview];

If you remove the previously loaded views, you always have 2 views loaded, so you cant access page-2 because you don't have page-2 views loaded in the scrollview . 如果删除先前加载的视图,则始终会加载2个视图,因此您将无法访问page-2,因为scrollview没有加载page-2的视图。 (think for example when page is 5, you will access view 3 from scrollview ) (例如,当页面为5时,您将从scrollview访问视图3)

You should consider 2 things: 您应该考虑两件事:

1.- Use [[[viewControllers objectAtIndex:page-2] view] removeFromSuperview]; 1.-使用[[[viewControllers objectAtIndex:page-2] view] removeFromSuperview]; instead (take a look that here I use viewcontrollers instead of scroll.subviews ) 相反(看看这里我使用viewcontrollers而不是scroll.subviews

2.- Remove the view from the viewcontrollers array , because just removing the view from the scroll will not release the view from memory (its retained by the array). 2.-从viewcontrollers array删除该view ,因为仅从滚动中删除该视图不会从内存中释放该视图(数组保留该视图)。 For this, use something like: 为此,请使用类似:

[viewControllers replaceObjectAtIndex:page-2 withObject:[NSNull null]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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