单个UICollectionView的多个单元格

3 uicollectionview swift

在我的集合视图中,单元格类必须具有完全不同的外观,以用于数组中的不同类型的数据.

我正在寻找一种方法来创建多个单元格,并根据输入数据选择不同的单元格,例如:

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell1 = collectionView.dequeueReusableCell..  as! kind1
    let cell2 = collectionView.dequeueReusableCell.. as! kind2
   // here choose a different one for each kind of data
    return cell1
}
Run Code Online (Sandbox Code Playgroud)

我试图了解是否:

  1. 如何做到这一点,如果它在内存方面是正确的方式?
  2. 这是一个好的设计吗?你将如何制作一个完全不同的单个细胞布局?(创建视图并隐藏它们似乎是一种糟糕的方法)

J. *_*ush 5

你需要做这样的事情

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if (data.kind == kind1) {
        let cell1 = collectionView.dequeueReusableCell..  as! kind1

        return cell1
    } else {
        let cell2 = collectionView.dequeueReusableCell.. as! kind2

        return cell2
    }
}
Run Code Online (Sandbox Code Playgroud)

通过检查数据的类型,您可以确定单元格.