如何解决如何在UICollectionView中具有自定义单元格
我有一个UICollectionView,我想显示3个自定义单元格。
我已经阅读了文档,但无法解决此问题。
有什么我想念的吗?
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return 1
}
我尝试将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
}
}
解决方法
在使用它之前,我们必须将三个不同的自定义单元格注册到collectionView中,然后在此函数内部添加此代码
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(indexPath.item==0)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1",for: indexPath) as! cell1
return cell1
}
if(indexPath.item==1)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2",for: indexPath) as! cell2
return cell2
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell3",for: indexPath) as! cell3
return cell3
}
,
我们从Apple的documentation中可以看到
您通常不会自己创建此类的实例。相反,您可以使用单元格注册来注册特定的单元格子类(或包含类的已配置实例的nib文件)。当需要单元格类的新实例时,请调用集合视图对象的dequeueConfiguredReusableCell(using:for:item :)方法来检索一个。
我们必须在使用之前将单元格注册到collectionView,例如:
class CustomCollectionViewCell: UICollectionViewCell {
// my custom collection view cell
}
然后我们将其注册到集合视图:
class MyViewController: UIViewController {
...
override func viewDidLoad(){
super.viewDidLoad()
...
self.myCollectionView.dataSource = self
// register the cells,so the collectionView will "know" which cell you are referring to.
self.myCollectionView.register(UINib(nibName: "CustomCollectionViewCell",bundle: nil),forCellReuseIdentifier: "customReuseIdentifier")
// register all type of cell you wanted to show.
}
}
extension MyViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
// return number of cell you wanted to show,based on your data model
return 3
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = routineCollectionView.dequeueReusableCell(withReuseIdentifier: "customReuseIdentifier",for: indexPath) as! CustomCollectionViewCell
// cast the cell as CustomCollectionViewCell to access any property you set inside the custom cell.
// dequeue cell by the reuseIdentifier,"explain" to the collectionView which cell you are talking about.
return cell
}
}
上面的代码片段只是一个简单的示例,但我希望能对此想法进行解释。
如果您有多种类型的自定义单元格,则必须为其创建类(UICollectionViewCell的子类),将它们注册到您的collectionView,然后将它们从collectionView(cellForRowAt :)中出队。
互联网上有很多教程,这里分享我最喜欢的一个: https://www.raywenderlich.com/9334-uicollectionview-tutorial-getting-started
编辑:
如果您仅使用情节提要来添加自定义collectionViewCell,则无需再次注册该单元格,因为该单元格已经存在于collectionView中(对不起,上面的代码只是我的偏好)。只需设置单元格的类和标识符,然后使用collectionView(cellForRowAt :)中的标识符使单元出队。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。