UITableViewCell复制值

时间:2019-01-21 10:30:04

标签: ios swift firebase uitableview

我正在使用Firebase和外部API来获取UITableView的数据。 通常我有这段代码。

public static func getUserLayers(completion: @escaping ()->Void){
    Firestore.firestore().collection("layers").whereField("uid", isEqualTo: (Auth.auth().currentUser?.uid)!).addSnapshotListener { (snapshot, error) in
        if error == nil {
            self.layers.removeAll()
            if let snap = snapshot?.documents {
                for doc in snap {
                    let data = doc.data()
                    let provider = data["layerprovider"] as! Int
                    if(provider != 0){
                        Alamofire.request("\(self.providerBaseUrl)\(String(describing: provider))").responseString { (response) in
                            if let resp = response.value {
                                let json = JSON(parseJSON: resp)
                                let logo = json["logo"]["medium"]["url"]
                                let blue = json["bg"]["blue"].intValue
                                let green = json["bg"]["green"].intValue
                                let red = json["bg"]["red"].intValue
                                self.layers.append(
                                    LayerInfo(ean: data["ean"] as! String, name: String(describing: data["name"]), logo: Provider.getLogo(imageUrl: "\(logo)"), bgR: red, bgG: green, bgB: blue, provider: json)
                                )
                            }
                            completion()
                        }

                    }else{
                        self.layers.append(
                            LayerInfo(ean: data["ean"] as! String, name: String(describing: data["name"]), logo: UIImage(), bgR: 0, bgG: 0, bgB: 0)
                        )
                    }
                }
                completion()
            }
        }
    }
}

那是从Firebase获取数据的代码,然后尝试获取不等于“ 0”的提供程序。

ViewController在这里是

LayerService.getUserLayers {
            self.layers = LayerService.layers
            self.tableView.reloadData()
        }

还有UITableViewCell:

DispatchQueue.main.async {
        for subview in self.subviews{
            subview.removeFromSuperview()
        }

        let layer = Layer(frame: CGRect(x: 10, y: 5, width: self.bounds.width - 40, height: 210), bg: self.bg, logo: self.logo, ean: self.ean)
        self.addSubview(layer)
    }

但是问题是,在重复使用单元格期间-它重复了第1和第5个单元格,第2和第6个单元格等。 甚至当我单击单元格#1时,它也会在单元格#5处翻转图层(因为我已经在Layer UIView子类上进行了轻按)...

我哪里错了?

谢谢。任何建议表示赞赏。

我的cellForRowAt方法

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! LayersTableViewCell 
    cell.logo = self.layers[indexPath.row].logo 
    cell.bg = self.layers[indexPath.row].bg 
    cell.ean = self.layers[indexPath.row].ean 
    return cell 
}

1 个答案:

答案 0 :(得分:0)

请在解析数据后重新加载表格视图

public static func getUserLayers(completion: @escaping (AnyObject)->Void){
Firestore.firestore().collection("layers").whereField("uid", isEqualTo: (Auth.auth().currentUser?.uid)!).addSnapshotListener { (snapshot, error) in
        if error == nil {
            self.layers.removeAll()
            if let snap = snapshot?.documents {
                for doc in snap {
                    let data = doc.data()
                    let provider = data["layerprovider"] as! Int
                    if(provider != 0){
                        Alamofire.request("\(self.providerBaseUrl)\(String(describing: provider))").responseString { (response) in
                            if let resp = response.value {
                                let json = JSON(parseJSON: resp)
                                let logo = json["logo"]["medium"]["url"]
                                let blue = json["bg"]["blue"].intValue
                                let green = json["bg"]["green"].intValue
                                let red = json["bg"]["red"].intValue
                                self.layers.append(
                                    LayerInfo(ean: data["ean"] as! String, name: String(describing: data["name"]), logo: Provider.getLogo(imageUrl: "\(logo)"), bgR: red, bgG: green, bgB: blue, provider: json)
                                )
                            }
                            completion(["res": self.layers, "stats":1] as AnyObject?)
                        }

                    }else{
                        self.layers.append(
                            LayerInfo(ean: data["ean"] as! String, name: String(describing: data["name"]), logo: UIImage(), bgR: 0, bgG: 0, bgB: 0)
                        )
                    }
                }
                completion(["res": self.layers, "stats":1] as AnyObject?)
            }
        }
    }

}

然后修改:

LayerService.getUserLayers (withCompletionHandler:AnyObject?) in{
        let dictResponse = withCompletionHandler as! Dictionary<String, Any>
            let isSuccess : Int = dictResponse["stats"] as! Int

            if isSuccess == 1{
              self.layers = LayerService.layers
              DispatchQueue.main.async {
                self.tableView.reloadData()
              }
            }

    }

这可能会对您有所帮助。谢谢