ios - 如何在 tableview swift 中追加行?

标签 ios swift tableview

我正在我的模型中添加数据,并且模型已分配给 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/

相关文章:

ios - Xcode 8 在启动时挂起; [MT] DVTAsertions 警告

xcode - 从当前位置知道 MapView 中最近的引脚

ios - 启动应用程序后如何重新加载 tableview 如果任何新内容每次都出现在数组中并且如果数组相同则不需要重新加载

ios - 如何仅将 swift 函数公开给 objc

iphone - 在 iOS 中使用耳机插孔同时播放和录制音频?

ios - 如何循环遍历具有自定义单元格的 TableView 并从自定义单元格获取信息?

ios - 自定义 UITableViewCell 只显示一个 UILabel,即使 Storyboard上有两个

ios - 将RTF文件解析为NSArray- objective-c

iphone - 下载文件时,NSURLConnection 不会在文件末尾调用 didFinishLoading,当下载暂停并恢复时

objective-c - 等到 UITableView 完成 reloadData