首页 > 解决方案 > 如何在tableview swift中追加行?

问题描述

我在我的模型中添加数据,模型被分配给 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()

这是因错误而崩溃:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(0),加上或减去插入或删除的节数(0插入,0已删除)。

标签: iosswifttableview

解决方案


您应该将insertSections用于新部分。insertRows 仅适用于现有部分。


推荐阅读