ame*_*ian 1 uitableview ios swift
有经验的人可以告诉我在swift避免冻结时从UITableView执行reloadData()的最佳方法是什么?
我有一个带有TableView的ViewController,它显示了一对10行的用户列表.当滚动显示最后一行 - 1,在后台,应用程序请求接下来的10个用户,然后将它们添加到其余用户,以便在TableView中显示20个用户.
当使用委托方法执行此操作时,重新加载会导致冻结约1~2秒并且导航不舒适.
有什么想法解决这个问题?
小智 7
当新数据到来时,您不需要重新加载整个tableView.您只需要相应地插入新行.这不会导致任何滞后/冻结.
func didFinishLoadNewUsers(newUsers: [User]) {
tableView.beginUpdates()
//array of index paths for new rows at the bottom
var indexPaths = [NSIndexPath]()
for row in (currentUsers.count..<(currentUsers.count + newUsers.count)) {
indexPaths.append(NSIndexPath(forRow: row, inSection: 0))
}
//update old data
currentUsers.appendContentsOf(newUsers)
//insert new rows to tableView
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3023 次 |
| 最近记录: |