UICollectionView ‘NSInternalInconsistencyException’ non-existent index path trouble

Hi all! I’ve seen problems similar to this on this forum and similar ones—but despite combing through other peoples’ solutions I haven’t been able to figure out my own. This greatest problem is that I’m getting a “Thread 1: signal SIGABRT” error, and the relevant line in the debugger seems to be “NSInternalInconsistencyException’, reason: ‘UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 0 – 1}“. My app is fairly simple: a kind of calculator for rolling dice. This error comes up once the collection view that shows the current dice has been cleared and then there is an attempt to add another one. In trying to keep this question efficient, here’s the code that (I believe) is most relevant:


UICollectionView Functions

extension MainController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    // TODO: Why are these getting called twice sometimes?
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tower.amountOfDieTypes
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DieCell", for: indexPath) as! DiceCollectionViewCell
        let dieInfo = tower.getDie(atArrayPosition: indexPath.row)
        cell.dieText = "(dieInfo!.amount)(dieInfo!.type)"
        return cell
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)

Relevant IBAction Functions

@IBAction func dieButtonPressed(_ sender: UIButton) {
        let numOfSides = Int(sender.tag)
        let newDie = Die(numOfSides)
        // TODO: This still isn't working quite right! The vertical alignment seems to not be working at random.
        // Update UI
        uiDispatchGroup.notify(queue: DispatchQueue.main) {
            self.diceCollectionView.contentInset.top = max((self.diceCollectionView.frame.height - self.diceCollectionView.contentSize.height) / 2, 0)
    @IBAction func rollButtonPressed(_ sender: UIButton) {
        if let result = tower.cumulativeRoll() {
            resultDisplayLabel.text = String(result)
    @IBAction func clearButtonPressed(_ sender: UIButton) {
        resultDisplayLabel.text = ""

And I suppose it would be useful to see the relevant functions in the “Tower” class that stores the dice information:


Tower Functions (a few)

public mutating func addDie(_ die: Die) {
        let position = dieTypePositionInArray(die)
        if position == -1 {
            dice.append((type: die, amount: 1))
        } else {
            dice[position].amount += 1

public func getDie(atArrayPosition position: Int) -> (type: Die, amount: Int)? {
        return dice[position]
public mutating func clearDice() {
        if !dice.isEmpty {
            dice = [(type: Die, amount: Int)]()


Please let me know what other information would be helpful! I just want to be mindful that I’m not overloading this initial post with unneccessary information. I’ve seen more than one solution to this problem that uses the collectionViewLayout.invalidateLayout() function, but that doesn’t seem to be doing anything for me (in line 6 of the IBAction functions block that I’ve included). An auxiliary question that could be related, is that once there are two dice represented in the collection view, my UICollectionView functions are called twice every time I try to add another die. Meaning, that print function in line 5 of the UICollectionView functions block is called twice, as well as the other functions that I’d expect would only be called once. Any thoughts on this? Thanks so much for your time, let me know how I can provide more relevant information!

Powered by WPeMatico


You may also like...

Comments are closed.