我想用两个不同的单元格创建一个collectionView。第一个单元格应该显示一次,而第二个单元格应该在数组很大时显示一次。结果应该类似于附件链接中的图像。这也是一个示例代码,用于更好地理解我的问题。感谢所有帮助我的人!!! 🙂
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch indexpath { // No indexpath in numberofitemsinsection!
case 0:
return 1 //display one time the first cell
default:
return images.count // display as often as the array is large the second cell
}
}
您为什么不使用它?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1 + theArray.count
}
您可以在cellForItemAt
内部实现此功能,并且需要像下面这样更改numberOfItemsInSection
:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// return 1 more than our data array (the extra one will be the "add item" cell)
return dataSourceArray.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// if indexPath.item is less than data count, return a "Content" cell
if indexPath.item < dataSourceArray.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.customWishlistTapCallback = {
}
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
//MARK: addList-Cell-Tapped
// set the closure
cell.tapCallback = {
}
return cell
}
为此,您需要创建ContentCell
和AddItemCell
,并且还具有dataSourceArray
来存储所需的所有数据。