uitableview - 如何实现 TableView.insertRows

标签 uitableview swift3

我正在为 myTableview 添加分页显示博客文章。数据源是一个帖子数组,即 posts = [post] .

我最初获取了 20 个帖子。我有一个按钮可以获取接下来的 20 条记录。所有这些都运行良好。我不知道如何在不调用 reloadData() 的情况下在表中插入这些新记录.有人可以解释以下代码吗?我不明白 indexPaths 发生了什么事情在下面的第 2 行和第 3 行。
在:

IndexPath(row:(self.posts.count - 1)

我是通过原始数据集的最后一行还是更新后的数据集?
TableView.beginUpdates()
let indexPath:IndexPath = IndexPath(row:(self.posts.count - 1), section:0)
TableView.insertRows(at: [indexPath], with: .left)
TableView.endUpdates()

最佳答案

如果您想向 tableview 添加项目,则传递给 insertRows 的值将是模型对象中新行的索引路径数组:

let additionalPosts = ...
posts += additionalPosts

let indexPaths = (posts.count - additionalPosts.count ..< posts.count)
    .map { IndexPath(row: $0, section: 0) }

tableView.insertRows(at: indexPaths, with: .left)

因此,如果您的数组中有 20 个项目,并且您又添加了 20 个帖子,则 indexPaths将是:

[[0, 20], [0, 21], [0, 22], [0, 23], [0, 24], [0, 25], [0, 26], [0, 27], [0, 28], [0, 29], [0, 30], [0, 31], [0, 32], [0, 33], [0, 34], [0, 35], [0, 36], [0, 37], [0, 38], [0, 39]]

关于uitableview - 如何实现 TableView.insertRows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43453220/

相关文章:

iphone - 当选择 UITableView 中的行时,使 UINavigationBar 中的完成按钮出现

c# - 执行从 UITableView 到新 View Controller Xamarin 的 segue

ios - 当使用多个部分时,当部分数量可能发生变化时,如何对 tableView 单元格的删除/添加进行动画处理

swift - 具有 UserDefaults 的全局数据 - Swift

ios - 将数据传递到表格单元格

swift - UITextField Swift 中只允许使用字母和空格

ios - 在 Swift 中组合两个数组以形成带有部分的表

swift - 使用perfectLib错误 '' ld : library not found for -lCOpenSSL for architecture x86_64"

json - swift 3 将 json 参数发布到 api

iphone - Autorelease 上的自定义 UITableViewCell 导致应用程序崩溃