我正在我的模型中添加数据,并且模型已分配给 tableview 以重新加载数据。但是每次重装都不好看。所以我只想要添加到模型中的最后一个元素,应该附加到已经存在的 TableView 中。尝试了很多方法,但当我的 tableview 为空时崩溃。
let lastSectionIndex = self.isGroupChat ? self.objGroupChatList!.count-1 : self.objSingleChatList!.count-1
var lastRow = 0
if self.isGroupChat {
lastRow = (self.objGroupChatList?[lastSectionIndex].count ?? 1)
} else {
lastRow = (self.objSingleChatList?[lastSectionIndex].count ?? 1)
}
let IndexPathOfLastRow = IndexPath(row: lastRow-1, section: lastSectionIndex)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [IndexPathOfLastRow], with: UITableViewRowAnimation.none)
self.tableView.endUpdates()
这是因错误而崩溃:
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 (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
最佳答案
你应该使用 insertSections对于新的部分。 insertRows 仅适用于现有部分。
关于ios - 如何在 tableview swift 中追加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217115/