我正在尝试使用两种方式向 UITableView 添加新数据
第一种方式
func insertData(appendMessages:[Message]) { var currentCount = self.messeges.count; var indxesPath:[NSIndexPath] = [NSIndexPath]() for msg in appendMessages { indxesPath.append(NSIndexPath(forRow:currentCount,inSection:0)); self.messeges.append(msg) currentCount++ } self.tableView.beginUpdates() self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom) self.tableView.endUpdates() }
第二种方式
func insertData(appendMessages:[Message]) { for msg in appendMessages { self.messeges.append(msg) } self.tableView.reloadData() }
你看我有分离的结果。
当使用“reloadData”时一切正常,但我想这不太好,因为我正在更新所有内容而不是新内容?
当使用“insertRowsAtIndexPaths”时,我有重复的分隔符,只有当我点击它时才会绘制该行
这很奇怪,我做错了什么......
谢谢
谢伊
最佳答案
错误是你应该将计数器初始化为 0 然后增加它:
func insertData(appendMessages:[Message]) {
var currentCount = 0
var indexesPath = [NSIndexPath]()
for _ in appendMessages {
let index = let index = NSIndexPath(forRow: i, inSection: 0)
indexesPath.append(index)
currentCount++
}
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(indexesPath, withRowAnimation: UITableViewRowAnimation.Bottom)
self.tableView.endUpdates()
}
关于ios - 将数据追加到 UITableView 的正确方法,swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25513667/