Nazmul Hasan
Nazmul Hasan

Reputation: 10610

Invalid update: invalid number of sections

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).'

but i insert 1 and deleted one based on data source what i missed

    self.states?.append(sortedStates) //Update state  property
    if (self.states?.count)! > 3 {
        self.states?.removeFirst()
    }
  self.newsFeedTableView.beginUpdates()
  self.newsFeedTableView.insertSections([(self.states?.count)! - 1], with: .none)
  if (self.states?.count)! > 3 {           
      let statesForoldestStateTime = self.states?.first
      self.newestStateTime = statesForoldestStateTime?.first?.createdAt
      let indexpostion = (self.states?.count)! - 3
     self.newsFeedTableView.deleteSections([indexpostion], with: UITableViewRowAnimation.none)
    }
  self.newsFeedTableView.endUpdates()

Upvotes: 6

Views: 14499

Answers (1)

Mohammad Sadiq
Mohammad Sadiq

Reputation: 5241

The error says it all. When if (self.states?.count)! > 3 is false. The only section would be inserted and not deleted. You should update your data source accordingly. The number of sections method must return someArray.count. When you insert some section, make sure to update that some array, and when you delete some section, delete the element from some array. That will resolve the issue.

Upvotes: 1

Related Questions