我有一个方法可以从 FireBase 获取数据,将其保存在本地并将其加载到 UITableView 中。效果很好。
现在,这个观察者 (rootGenerals.observe()) 实时运行并在每次服务器上的数据发生变化时进行更新。
我的问题是我不想在获得新数据后重新加载整个表(就像我现在做的那样),我只想更新表中特定的更改行或替换行位置(IndexPath ) 如果分数发生变化。
知道怎么做吗?只需要一些指导,了解在这种情况下最好的处理方式。
rootGenerals.observe(.value, with: { snapshot in
if !snapshot.exists() { return }
let general = snapshot.value as! NSDictionary
if let users : NSDictionary = general.object(forKey: "users") as? NSDictionary
{
if (self.usersFromDB?.count != 0) {
// Update table rows
self.arrRecords = []
}
else {
// Create table from scratch
self.usersFromDB = users
for user : Any in users.allKeys {
let userDict : NSDictionary = users.object(forKey: user as! String) as! NSDictionary
let record : Record = Record()
record.displayName = userDict.object(forKey: "name") as! String
record.time = userDict.object(forKey: "time") as! Int
record.solvedLogos = userDict.object(forKey: "logos_solved") as! Int
record.deviceId = userDict.object(forKey: "device_id") as! String
record.raw = userDict
self.arrRecords.append(record)
}
self.sortRecords()
}
}
})
最佳答案
如果你想更新特定的索引路径使用
tableView.reloadRows(at: your_indexpath_array, with: your_Animation)
在表中插入新行
tableView.beginUpdates()
tableView.insertRows(at: your_indexpath_array, with: your_Animation)
tableView.endUpdates()
关于ios - 从服务器 swift 更新数组中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746163/