有没有办法使用 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)
上面从底部插入行,有没有办法从顶部插入?
最佳答案
索引路径来自
NSIndexPath(forRow: self.mileageDatabase.count - 1, inSection: 0)
将新行添加到 TableView 的底部,假设 mileageDatabase 代表 TableView 的数据源。由于您总是希望将新行插入到顶部,因此需要将索引路径设置为 0。假设您的 TableView 只有一节,您需要如下定义索引路径:
NSIndexPath(forRow: 0, inSection: 0)
您还需要在快照
前面添加而不是附加它,否则您的数据源将与您的 UI 表示(表格 View 中显示的内容)不同步。
关于从顶部快速插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181883/