ios - 从服务器 swift 更新数组中的更改

标签 ios swift uitableview firebase

我有一个方法可以从 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/

相关文章:

iphone - 重新加载 TableView 数据并取消选择单元格

ios - 从 UIPickerView 设置 UITextField 中当前列的标题?

ios - 我无法在 Xcode 12 的 xib 中创建自定义 UIView

ios - 使用 Alamofire 和 EVReflection 的 JSON POST 请求

ios - UICollectionView inside UITableViewCell default contentOffset

iOS/ swift 3.0 : how do you determine which rows are currently visible in a UITableView?

iphone - iOS:如何生成 8 个唯一的随机整数?

ios - 在对 tableView 约束进行动画处理时,TableViewCells 最初的大小错误

swift - 防止结合 flatMap 中的自保留循环

ios - DestinationViewController Segue 和 UINavigationController swift