我需要创建一个 UIcollectionview,其中包含每行 3 个单元格的单元格网格。在一定数量的单元格(比如说 60 个)之后,我需要显示一个带有 ImageView 单元格 61(不同数据集)的大单元格。但是,通过创建图像的第 61 个单元格,我第 61 个单元格的正常数据将合并到此图像单元格中,有什么方法可以在不影响我的数据数组的情况下计算它。谢谢。
最佳答案
这是数据源部分:
var items = [Item]()
func numberOfSections(in collectionView: UICollectionView) -> Int {
let numberOfSections = 60/(items.count+1) + 1
return numberOfSections
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let numberOfSections = 60/(items.count+1) + 1
if section != numberOfSections - 1 {
return 60
}
let numberOfItems = items.count%60
return numberOfItems
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let itemIndex = indexPath.section * 60 + indexPath.row
let item = items[itemIndex]
// set cell from item …
}
然后您需要使用 UICollectionViewDelegate 提供标题单元格:
func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath)
搜索“willDisplaySupplementaryView”将向您展示如何实现这一点。祝你好运!
关于ios - UIcollectionview在特定行后显示不同的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880984/