添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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()
                Not related but consider to consolidate your incredible amount of question marks.  For example why is states optional at all?
– vadian
                Jul 23, 2017 at 14:54
                In most cases it's more suitable to declare collection types as an empty non-optional type var states = [[State]]() especially for data source arrays.
– vadian
                Jul 23, 2017 at 15:07

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.