iOS:具有许多 UIViewController 的 UIScrollView 中的内存管理

chr*_*aso 1 uiscrollview uiviewcontroller ios

我有一个关于启用分页的 UIScrollView 的问题,其中包含许多 UIView,每个 UIView 由自己的 UIViewController 管理。

现在大约有 20 到 30 个 UIViewControllers 可以包含在 UIScrollView 中。它是 iPad 上的目录应用程序,一开始我就开始预加载所有视图,但是随着 UIViewControllers 的数量越来越大,这不再是一种选择。

我正在寻找内存使用方面的完美解决方案。当 ScrollView 的 ContentOffset 到达特定控制器时,重新加载 UIViewControllers 没有问题。而且我认为当 ContentOffset 告诉我不再需要 UIViewControllers 时,将 UIViewControllers 置零也不是那么难。

处理这个问题的正确方法是什么?在需要时分配 UIViewControllers 是否足够,将它们放入 NSMutableDictionary 或 NSMutableArray 并在不再需要它们时将它们归零?已经做过类似事情的人的一点帮助会很棒!

谢谢你的帮助!

Rob*_*Rob 5

我敢肯定那里有一些很好的无限滚动类,但是如果您要“滚动自己的”,这里有一段极简的代码,演示了无限滚动的过程,保留当前页面、上一页和下一页在记忆中,但放弃任何其他东西。这假设:

  • 您正在水平滚动并打开分页;
  • 您正在为子视图使用视图控制器;
  • 你的子视图控制器类有一个page属性来跟踪它的页面;和
  • 你已经让你的视图控制器成为你滚动视图的代表

因此,它可能看起来像:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // my underlying model is just an array of strings, which I'll show on my child
    // view; your model will be more elaborate, but I just want to illustrate the concept

    self.objects = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"];

    // set the `contentSize` for the scrollview

    CGRect content = self.view.bounds;
    content.size.width *= [self.objects count]; // make it wide enough to hold everything
    self.scrollView.contentSize = content.size;

    // set our current page and load the first pages (the first and the next pages)

    self.currentPage = 0;
    [self addChildPage:0 toScrollView:self.scrollView];
    [self addChildPage:1 toScrollView:self.scrollView];
}

- (void)addChildPage:(NSInteger)page toScrollView:(UIScrollView *)scrollView
{
    // create the child controller

    ChildViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"child"];

    // set whatever properties you need to in order for it to present its information correctly

    controller.text = self.objects[page];
    controller.page = page;

    // now do the stuff to add it to the right place in the scrollview

    CGRect frame = self.view.bounds;
    frame.origin.x = frame.size.width * page;
    controller.view.frame = frame;
    [self addChildViewController:controller];        // containment call for adding child view controller
    [scrollView addSubview:controller.view];
    [controller didMoveToParentViewController:self]; // containment call when done adding child
}

- (ChildViewController *)childControllerForPage:(NSInteger)page
{
    for (ChildViewController *controller in self.childViewControllers)
    {
        if (controller.page == page)
            return controller;
    }

    return nil;
}

- (void)addChildIfNecessary:(NSInteger)page toScrollView:(UIScrollView *)scrollView
{
    if (page < 0 || page >= [self.objects count])
        return;

    ChildViewController *controller = [self childControllerForPage:page];

    if (controller == nil)
        [self addChildPage:page toScrollView:scrollView];
}

- (void)removeChildController:(UIViewController *)controller
{
    [controller willMoveToParentViewController:nil];  // containment call before removing child
    [controller.view removeFromSuperview];
    [controller removeFromParentViewController];      // containment call to remove child
}

- (void)updateChildrenViewsForPage:(NSInteger)page forScrollView:(UIScrollView *)scrollView
{
    if (page == self.currentPage)
        return;

    // add child pages as necessary

    [self addChildIfNecessary:page     toScrollView:scrollView];
    [self addChildIfNecessary:(page-1) toScrollView:scrollView];
    [self addChildIfNecessary:(page+1) toScrollView:scrollView];

    // find any pages that need removing

    NSMutableArray *pagesToRemove = [NSMutableArray array];
    for (ChildViewController *controller in self.childViewControllers)
    {
        if (controller.page < (page - 1) ||
            controller.page > (page + 1))
        {
            [pagesToRemove addObject:controller];
        }
    }

    // remove them if they need removing

    for (UIViewController *controller in pagesToRemove)
    {
        [self removeChildController:controller];
    }

    // update our "current page" index

    self.currentPage = page;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;

    [self updateChildrenViewsForPage:page forScrollView:scrollView];
}
Run Code Online (Sandbox Code Playgroud)

这演示了适当的自定义容器调用和滚动事件的处理。我希望这有帮助。

  • 谢谢罗布。这是一个非常详细的答案,对我有很大帮助! (2认同)