この問題にさまざまな方法で対処しようとしています。 「NSInvalidArgumentException」、理由: 'インデックス0のセクションのインデックス3でノーオブジェクトの それはクラス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のように思えます複数のセクションがあり、すべてのセクションには追加ボタンがあります。この仮定は正しいですか?または、セクションの1つだけに追加ボタンが必要ですか? – Zhang
@ Zhangセクションのみ。 1つのセクションの最後にボタンを追加します。 –