ios - 如何在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已删除)。
解决方案
您应该将insertSections用于新部分。insertRows 仅适用于现有部分。
推荐阅读
- tcp - TCP 推送数据包未从 tun 传递
- python - 确定顶点是否位于一组顶点内
- php - php shuffle 关联数组的一部分
- javascript - 使用 HighCharts 动态更新同一图表上的两个系列
- javascript - 使用 BehaviorSubject 重新加载刷新期间的角度视图组件数据丢失
- r - 使用 usmap 功能去除县界
- html - 中心牌但有左对齐?
- jqgrid - jqGrid,为动态下拉内联编辑设置初始值
- c# - 无法加载类型“IdentityModel.Client.DiscoveryClient”
- javascript - ReactJS 组件不显示