具有多个不同单元格的 UICollectionView,在其他 CollectionView 中注册

CollectionView with multiple different cells, registered in other CollectionViews

我有两个独立的视图控制器。

ViewController1 中,我有一个 CollectionView1 和一个工作正常的单元格。我使用 storyboardReuseIdentifier "Cell1".

设置单元格

ViewController2 中,我有另一个 CollectionView2 和一个工作正常的不同单元格。我还使用 storyboardReuseIdentifier "Cell2".

设置了单元格

两个单元格都有自定义单元格 类、UserCell1UserCell2,并且在各自的 CollectionViews.

中工作正常

现在,对于 CollectionView2 中的第二个单元格,我想使用第一个集合视图中的单元格 (ie. IndexPath(row: 1, section: 0))

CollectionView2,我想要:

IndexPath(row: 0, section: 0) -> "Cell1"
IndexPath(row: 1, section: 0) -> "Cell2"

ViewController2 上,在 cellForItemAt 中,我尝试使用公式指示在每个 index 处要 dequeue 的单元格。但是,它告诉我来自第一个 ViewController 的单元格未注册...但同一个单元格正在其家中工作 CollectionView,因此 ReuseIdentifier 显然正在工作。

cellForItemAt (numberOfRowsInSection = 2):

之内
if indexPath == IndexPath(row: 0, section: 0) {
                guard let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as? UserCell1 else { return UICollectionViewCell() }
                cell1.configure(user: selectedUser)
                return cell1
            } else if indexPath == IndexPath(row: 1, section: 0) {
                guard let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as? UserCell2 else { return UICollectionViewCell() }
                cell2.configure(user: selectedUser)
                return cell2
            } else {
            return UICollectionViewCell()
            }

我得到的错误是:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell1 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

提前致谢!!!!这让我发疯:(

代码的最佳做法是在 XIB 中而不是故事板中定义每个单元格,这将帮助您通过从代码中注册它们来在任何集合视图中重用这两个单元格,您可以在此处查看示例:

如果出于某种原因需要在故事板中定义单元格,则需要在每个 collectionView2 中声明两个原型单元格

请看下面的屏幕,我们有一个项目数 = 2 的集合视图,我们应该为每个单元格提供唯一的 ReuseIdentifier

要在 UICollectionView 中使用 UICollectionViewCell,您必须先 register cellcollectionView

为什么 Cell1CollectionView1 而不是 CollectionView2

如果您在使用的 collectionView 中创建了 cell,则可以跳过 registering cell 部分。但是如果你想在其他一些 collectionView 中使用它,registering the cell 是强制性的。这就是 Cell1CollectionView1 而不是 CollectionView2 中完美运行的原因。

因为你想在CollectionView2中同时使用Cell1Cell2,所以cells都必须是registered

但问题是您在 storyboard.

中的 CollectionView1 本身中创建了 Cell1
  1. 所以首先您需要创建一个 .xib file 并将 Cell1 移入其中。让我们将 .xib file 命名为 Cell1.xib.

建议:您必须在 .xib file 中创建自定义 UICollectionViewCell,这样您也可以在其他地方使用它。

  1. 接下来您需要在 ViewController1 中使用 CollectionView1 注册 Cell1,在 viewDidLoad() 中使用 ViewController2 中的 CollectionView2 注册。

ViewController1中:

override func viewDidLoad() {
    super.viewDidLoad()
    CollectionView1.register(UINib(nibName: "Cell1", bundle: nil), forCellWithReuseIdentifier: "Cell1")
}

ViewController2中:

override func viewDidLoad() {
    super.viewDidLoad()
    CollectionView2.register(UINib(nibName: "Cell1", bundle: nil), forCellWithReuseIdentifier: "Cell1")
}

同样,您也可以使用 collectionView 注册其他类型的 cells。尝试实现它,如果您仍然遇到任何问题,请告诉我。