顶部插入, 且保持 偏移不变
{
self.dataArray.insert(contentsOf: results, at: 0)
self.listView.reloadData()
if results.count > 0 {
self.listView.scrollToRow(at: IndexPath(row: results.count - 1, section: 0), at: .top, animated: false)
}
}
底部插入行
func insertMessage(_ message: EMMessage) {
let oldDataCount = dataArray.count
let resultModels = formatMessage([message])
self.dataArray.append(contentsOf: resultModels)
var indexPaths = [IndexPath]()
for index in oldDataCount..<self.dataArray.count {
indexPaths.append(IndexPath(row: index, section: 0))
}
listView.performBatchUpdates {
self.listView.insertRows(at: indexPaths, with: .none)
} completion: { _ in
self.scrollToBottom()
}
}
网友评论