UICollectionView 中的 UIViewController [英] UIViewController within UICollectionView

查看:15
本文介绍了UICollectionView 中的 UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个全屏 UICollectionView.它水平滚动,每个单元格填充集合视图的边界.集合视图由 UIViewController 管理.

I have a full-screen UICollectionView in my app. It scrolls horizontally and each cell fills the collection view's bounds. The collection view is managed by a UIViewController.

鉴于每个页面"都相当复杂,每个页面本身由关联的 UIViewController 管理是有意义的.iOS 5 支持视图控制器包含,因此当视图被附加和分离时,子控制器应该接收适当的生命周期方法(例如 viewWillAppear: 等).这对视图回收有多好?

Given that each "page" is reasonably complex, it makes sense for each page itself to be managed by an associated UIViewController. iOS 5 has support for view controller containment, so that the child controllers should receive the appropriate lifecycle methods (e.g. viewWillAppear:, etc) when views are attached and detached. How nicely would this play with view recycling?

从页面1"滚动到2",将创建一个新视图(因为在触地期间两者可以同时出现在屏幕上).从页面2"移动到3",UICollectionView 可以成功地使页面1"的视图出列,但是现在会发生什么?我会像这样强行将视图插入到视图控制器 3 中吗?

Scrolling from page "1", to "2", a new view would be created (as both could be onscreen at the same time during the touch-down). Moving from page "2" to "3", the UICollectionView could successfully dequeue the view for page "1", but what happens now? Would I forcefully insert the view into view controller three like so?

id cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ident" forIndexPath:indexPath];
UIViewController *child_controller = [self controllerAtIndexPath:indexPath];
[child_controller setView:cell];
// ... and so on

这感觉不对.但是,我想不出在这种情况下正确重用视图的正确方法.我是否完全采取了错误的方法?

This feels wrong. However, I can't think of a correct way to reuse views correctly in this instance. Am I taking the wrong approach entirely?

推荐答案

我不认为 UICollectionView 是您任务的最佳选择.
就像 rob mayoff 说的:你可以使用 UIPageViewController.
另一种选择是将 UIScrollView 与 3 个子视图(上一个、当前、下一个)一起使用.并且您将能够轻松管理它的位置和滚动视图偏移以达到您想要的效果.
这在 WWDC 2011 高级滚动视图技术"中有所描述.您可以从那里获取源代码.

I don't think that UICollectionView is the best choose for your task.
Like rob mayoff said: you can use UIPageViewController.
Another option can be using UIScrollView with 3 subviews (previous, current, next). And you will be able to easy manage it's position and scroll view offset to achieve effect that you want.
This is described in WWDC 2011 'Advanced Scrollview Techniques'. You can grab source code from there.

这篇关于UICollectionView 中的 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆