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

时间:2019-05-23 23:47:46

标签: swift uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewflowlayout

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

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上,我尝试使用表示每个dequeueindex哪个单元格的公式。但是,这告诉我第一个ViewController中的单元未注册...但是同一单元在其主CollectionView中工作,因此ReuseIdentifier显然在工作。 / p>

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'

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

2 个答案:

答案 0 :(得分:0)

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

How to load custom cell ( xib) in UICollectionView cell using swift

如果由于某种原因需要在情节提要中定义单元格,则需要在每个collectionView2中声明两个原型单元格

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

enter image description here

答案 1 :(得分:0)

要在UICollectionViewCell中使用UICollectionView,必须先将registercell一起collectionView

为什么Cell1CollectionView1中工作而不在CollectionView2中工作?

如果您在使用的同一cell中创建了collectionView,则可以跳过registering cell部分。但是,如果要在其他collectionView中使用它,则必须使用registering the cell才能使它正常工作。这就是Cell1CollectionView1而不是CollectionView2正常工作的原因。

由于您想同时使用Cell1中的Cell2CollectionView2,因此cellsregistered都必须同时使用。

但是问题是您是在Cell1内的CollectionView1本身中创建了storyboard

  1. 因此,首先您需要创建一个.xib file并将Cell1移入其中。让我们将.xib file命名为Cell1.xib

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

  1. 接下来,您需要在Cell1本身的CollectionView1中向ViewController1注册CollectionView2,并在ViewController2中向viewDidLoad()注册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") } 中:

cells

类似地,您也可以向collectionView注册其他类型的select id, status,min(DAY) start,max(DAY) end from ( select *,island=row_number() over(partition by id order by day) - row_number() over(partition by id, status order by day) from tablename )A group by id, status,island 。尝试执行此操作,如果您仍然遇到任何问题,请告知我。