尝试以不同的方式解决此问题。 当前的实现给了我一个错误:' NSInvalidArgumentException',原因:'索引0处的索引3处没有对象' 它发生在AppDelegate类:UIResponder ......
我的代码是
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
configureCell(cell: cell, indexPath: indexPath)
var numberOfItems = self.collectionView(self.collectionView, numberOfItemsInSection: 0)
if (indexPath.row == numberOfItems - 1) {
var addCellButton = UIButton(frame: cell.frame)
addCellButton.setTitle("Add", for: UIControlState.normal)
cell.addSubview(addCellButton)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let sections = controller.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects + 1
}
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if let sections = controller.sections {
return sections.count
}
return 0
}
有什么建议吗?
答案 0 :(得分:3)
你可以这样写它以使两个单独的单元格出列:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var collectionView:UICollectionView!
var items = ["Apple", "Banana", "Orange", "Watermelon", "Coconut"]
...
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// ------------------------------------------
// +1 here for the extra add button
// at the bottom of the collection view
// ------------------------------------------
return items.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// --------------------------------
// IndexPath row vs Items Count
// --------------------------------
// [0] = Apple
// [1] = Banana
// [2] = Orange
// [3] = Watermelon
// [4] = Coconut
//
// [5] = special cell
//
// ---------------------------------
let cellID = indexPath.row < items.count ? "normalCell" : "specialCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
setupCell(cell: cell, indexPath: indexPath, type: cellID)
return cell
}
func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String) {
switch(type) {
case "normalCell":
setupFruitCell(cell: cell as! FruitCell, indexPath: indexPath)
case "specialCell":
setupSpecialCell(cell: cell as! SpecialCell, indexPath: indexPath)
default:
break
}
}
func setupFruitCell(cell: FruitCell, indexPath: IndexPath) {
cell.label.text = items[indexPath.row]
}
func setupSpecialCell(cell: SpecialCell, indexPath: IndexPath) {
cell.btnAdd.addTarget(self, action: #selector(addButtonTapped), for: UIControlEvents.touchUpInside)
}
func addButtonTapped(sender: UIButton) {
print("Show UI to add new fruit")
}
}
答案 1 :(得分:1)
不要通过添加视图来破坏您的单元格。当细胞离开屏幕时细胞会被回收(这就是为什么它被称为dequeueReusableCell)。除非你在准备重用时删除了额外的视图,否则它们会留在单元格中并重新添加。相反,您还有其他两个选择:
1)在接口构建器中使用自己的类和reuseIdentifier创建第二个collectionViewCell,并仅为最后一个单元格取消此单元格的实例。
2)使按钮成为页脚视图的一部分并在viewForSupplementaryElementOfKind
中返回答案 2 :(得分:0)
这就是我的解决方法:迅速4.2
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// +1 here for the extra add button
// bottom of the collection view
return item.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//initialization cell
let cells : UICollectionViewCell?
if indexPath.row < item.count {
// if indexpath.row < item.count => assign value to cellPet
let cellPet = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell1", for: indexPath) as! CollectionViewCell1
// setup data cell or not
cellPet.setupView(listPet: item[indexPath.row ])
// assign values cells
cells = cellPet
} else {
// assign value to cellAdd
let cellAdd = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell2", for: indexPath) as! CollectionViewCell2
// setup data cell or not setup
// assign values cells
cells = cellAdd
}
return cells!
}
答案 3 :(得分:-1)
你可以这样做
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
if indexPath.row < myArr.count {
cell.configure(with: CollectionModelViewCell(label: myArr[indexPath.row].text))
} else {
cell.configure(with: CollectionModelViewCell(label: "+"))
}
return cell
}