gpt4 book ai didi

ios - CollectionViewController,我应该使用普通的 UIViewController 子类化或实现它的委托(delegate)/数据源吗?

转载 作者:行者123 更新时间:2023-12-01 16:15:56 26 4
gpt4 key购买 nike

当我使用 CollectionView 实现 viewController 时, 是否应该拖拽CollectionViewController从storyBoard然后创建一个MyCollectionViewController类,它是 CollectionViewController 的子类?

或者我应该只使用一个普通的 viewController,向它添加一个 Collection View ,并实现它的 dataSource 和委托(delegate)协议(protocol)?

我知道以后的好处之一是您可能有多个 CollectionViews下一个ViewController ?但是还有哪些优点和缺点?

以及它如何应用于 TableViewController ,因为它们彼此非常相似。

最佳答案

UICollectionViewController 继承自 UIViewController,因此它本质上是一个带有 UICollectionView 和一些内置功能的 UIViewController。根据文档,此功能是:

If the collection view controller has an assigned nib file or was loaded from a storyboard, it loads its view from the corresponding nib file or storyboard. If you create the collection view controller programmatically, it automatically creates a new unconfigured collection view object, which you can access using the collectionView property.

When loading a collection view from a storyboard or nib file, the data source and delegate objects for the collection view are obtained from the nib file. If a data source or delegate is not specified, the collection view controller assigns itself to the unspecified role.

When the collection view is about to appear for the first time, the collection view controller reloads the collection view data. It also clears the current selection every time the view is displayed. You can change this behavior by setting the value of the clearsSelectionOnViewWillAppear property to NO.



我更喜欢始终继承 UIViewController 并在 Storyboard 中添加 UICollectionView。这意味着我的子类必须实现 UICollectionViewDataSource 和 UICollectionViewDelegate 协议(protocol),并且我必须在 Storyboard 中设置它们,并且我必须确保当 View Controller 出现时我重新加载 Collection View 。但是因为我自己实现了这个行为,所以感觉我有更多的控制权。如果您倾向于使用常规的默认行为,那么也许可以使用 UICollectionViewController 以节省时间。

我对带有 UITableView 的 UITableViewController 和 UIViewController 的感觉完全一样——出于完全相同的原因,我更喜欢后者。

关于ios - CollectionViewController,我应该使用普通的 UIViewController 子类化或实现它的委托(delegate)/数据源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27995849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com