Swift从顶部插入行

时间:2016-08-27 13:50:22

标签: swift firebase firebase-realtime-database

有没有办法使用insertRowsAtIndexPath从顶部插入行?我正在使用Firebase进行实施。

self.ref.child("mileage").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
            self.mileageDatabase.append(snapshot)
            self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.mileageDatabase.count - 1, inSection: 0)], withRowAnimation: .Automatic)

以上从底部插入行,是否有从顶部插入的方法?

1 个答案:

答案 0 :(得分:0)

的索引路径
NSIndexPath(forRow: self.mileageDatabase.count - 1, inSection: 0)

将新行添加到表视图的底部,假设mileageDatabase表示表视图的数据源。由于您始终希望将新行插入顶部,因此需要将索引路径设置为0.假设您的表视图只有一个部分,则需要将索引路径定义如下:

NSIndexPath(forRow: 0, inSection: 0)

您还需要添加snapshot而不是附加它,否则您的数据源将与您的UI表示不同步 - 表格视图中显示的内容。