[英]How to append row in tableview swift?
我在模型中添加數據,並將模型分配給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()
這崩潰並顯示錯誤:
由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無效的更新:無效的節數。 更新(1)之后表視圖中包含的節數必須等於更新(0)前表視圖中包含的節數,加上或減去插入或刪除的節數(插入的0,0刪除)。”
您應該將insertSections用於新部分。 insertRows僅適用於現有部分。
你需要做類似的事情,
let section = 0 //get your section here...
dataSource[section].append("five")
let row = dataSource[section].count - 1
tableView.insertRows(at: [IndexPath(row: row, section: section)], with: .none)
這只是如何使它工作的一個示例。 根據您的代碼填補空白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.