2

Trying to add a button "Add New" at the end of my UICollecitonView. Working with 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
}

Error: 'NSInvalidArgumentException', reason: 'no object at index 3 in section at index 0'

Do I need to check bounds of NSFetchedResultsController? As proposed here: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 3 in section at index 0'

Community
  • 1
  • 1
Mr_Vlasov
  • 515
  • 2
  • 6
  • 25
  • just a note: in `cellForItemAt` you add (mostly) always an `addCellButton` again and again, because the cell gets reused .... you should do this only once in a cell. later you can hide it or you should remove it. – muescha Jan 13 '17 at 06:55
  • i which line you get the error? – muescha Jan 13 '17 at 06:57
  • @muescha I get a general error at class AppDelegate: UIResponder, UIApplicationDelegate ... Tried to use prints and find out more about the error, but don't know where and how. – Mr_Vlasov Jan 13 '17 at 07:10
  • @muescha about the button - i want the button to be always at the end of CollectionView so I assume, it needs to be created every time, isn't it right? – Mr_Vlasov Jan 13 '17 at 07:28
  • No. The cell gets reused. So the reused cell still have the Button added from the previous use. You can Check this in the 3d debug view. You See only One Button because the other Buttons are Just below the new Button added – muescha Jan 15 '17 at 21:18
  • Try debug Mode to find ozt the line which trows the error – muescha Jan 15 '17 at 21:19

1 Answers1

1

You're telling the UICollectionView that you have one extra item in the section than is stored in the NSFetchedResultsController.

I believe the exception is being thrown when you try to access NSFetchedResultsController.object(at: IndexPath) from the configureCell method for that "extra" item you said was there.

If you want the Add New button to appear in the same type of cell as all the others, the you should stop adding the extra item in collectionView(numberOfItemsInSection:)

Alternatively, you can create a different type of cell to put at the end, with just the Add New button, and move the if statement before you dequeue the cell. If you're populating the last item, you dequeue your "AddNew" cell identifier with the button. Otherwise you dequeue the "Cell" identifier and configure it to display the data from the NSFetchedResultsController.

Dave Weston
  • 6,527
  • 1
  • 29
  • 44