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).'
但我根据我错过的数据源插入 1 并删除了
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()
最佳答案
错误说明了一切。当 if (self.states?.count)! > 3
为假。唯一的部分将被插入而不是被删除。
您应该相应地更新您的数据源。节数方法必须返回 someArray.count
。当您插入某些部分时,请确保更新该数组,而当您删除某些部分时,请确保从某个数组中删除该元素。这将解决问题。
关于ios - 无效更新 : invalid number of sections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266248/