Попытка решить эту проблему по-разному. Текущая реализация дает мне ошибку: «NSInvalidArgumentException», причина: «нет объекта с индексом 3 в разделе с индексом 0» Это происходит в классе AppDelegate: UIResponder ...Добавить кнопку «Добавить новую» в последней ячейке UICollectionCell. (CoreData)
Мой код
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
}
Любые советы?
От вашего кода, похоже, ваш CollectionView имеет несколько разделов, и каждый раздел имеет кнопку добавления в конце. Правильно ли это предположение? Или только в одной из разделов есть кнопка добавления? – Zhang
@ Чжан только один раздел. Добавить кнопку в конце одной секции. –