如何在 UICollectionView 中拥有自定义单元格

Irv*_*lez 1 ios uicollectionview uicollectionviewcell swift

我有一个 UICollectionView,我想让它显示 3 个自定义单元格。

我已阅读文档,但无法解决此问题。

我有什么遗漏的吗?

      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}
Run Code Online (Sandbox Code Playgroud)

我尝试将 return 1 更改为 3 以使 3 个自定义单元格出现,但它只使第一个自定义单元格出现 3 次。

我创建了一个视频并链接了下面的视频来解释我的情况。

https://www.loom.com/share/9b5802d6cc7b4f9a93c55b4cf7d435bb

编辑我使用了@Asad Farooq 方法,它似乎对我有用。我添加了如下所示的 CollectionView,现在我可以制作自定义单元格!

    if(indexPath.item==0)
    {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "DailyCollectionViewCell", for: indexPath) as! DailyCollectionViewCell
        return cell
    }
    if(indexPath.item==1)
    {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "WeeklyCollectionViewCell", for: indexPath) as! WeeklyCollectionViewCell
        return cell
        
    }
    else {
    
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "MonthlyCollectionViewCell", for: indexPath) as! MonthlyCollectionViewCell
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

pak*_*aky 5

从Apple的文档中我们可以看到,

\n
\n

您通常不会自己创建此类的实例。相反,您可以使用单元注册来注册特定的单元子类(或包含类的已配置实例的 nib 文件)。当您需要单元类的新实例时,请调用集合视图对象的 dequeueConfiguredReusableCell(using:for:item:) 方法来检索一个实例。

\n
\n

在使用之前我们必须将单元注册到collectionView,例如:

\n
class CustomCollectionViewCell: UICollectionViewCell {\n    // my custom collection view cell\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后我们将其注册到集合视图中:

\n
class MyViewController: UIViewController {\n    ...\n    override func viewDidLoad(){\n        super.viewDidLoad()\n        ...\n        self.myCollectionView.dataSource = self\n        // register the cells, so the collectionView will "know" which cell you are referring to.\n        self.myCollectionView.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellReuseIdentifier: "customReuseIdentifier")\n        // register all type of cell you wanted to show.\n    }\n\n}\n\nextension MyViewController: UICollectionViewDataSource {\n    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n        // return number of cell you wanted to show, based on your data model\n        return 3\n    }\n\n    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n        let cell = routineCollectionView.dequeueReusableCell(withReuseIdentifier: "customReuseIdentifier", for: indexPath) as! CustomCollectionViewCell\n        // cast the cell as CustomCollectionViewCell to access any property you set inside the custom cell.\n        // dequeue cell by the reuseIdentifier, "explain" to the collectionView which cell you are talking about.\n        return cell\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

上面的代码片段只是一个简短的例子,但我希望能够解释这个想法。

\n

如果您有多种类型的自定义单元格,则必须为它们创建类(UICollectionViewCell 的子类),将它们注册到您的collectionView,并在collectionView(cellForRowAt:) 中将它们出列。

\n

互联网上有很多教程,这里分享我最喜欢的一个:\n https://www.raywenderlich.com/9334-uicollectionview-tutorial-getting-started

\n

编辑:

\n

如果您仅使用故事板来添加自定义collectionViewCell,则不需要再次注册单元格,该单元格已经存在于collectionView中(抱歉,上面的代码只是我的偏好)。只需设置单元格的类和标识符,然后使用 collectionView(cellForRowAt:) 中的标识符使单元格出队。

\n