在正确的位置调用reloadData()

时间:2017-12-27 15:31:53

标签: ios swift uitableview

我正在尝试从firebase获取数据并传递给tableview。

 // Model
          import UIKit
          import Firebase

              struct ProfInfo {

                  var key: String
                  var url: String
                  var name: String

                     init(snapshot:DataSnapshot) {

                         key = snapshot.key
                         url = (snapshot.value as! NSDictionary)["profileUrl"] as? String ?? ""
                         name = (snapshot.value as! NSDictionary)["tweetName"] as? String ?? ""
                }
           }

   // fetch 
            var profInfo = [ProfInfo]()

            func fetchUid(){
                    guard let uid = Auth.auth().currentUser?.uid else{ return }
                    ref.child("following").child(uid).observe(.value, with: { (snapshot) in
                        guard let snap = snapshot.value as? [String:Any] else { return }
                        snap.forEach({ (key,_) in
                            self.fetchProf(key: key)
                        })
                    }, withCancel: nil)
                }

                func fetchProf(key: String){
                    var outcome = [ProfInfo]()
                        ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in
                                let info = ProfInfo(snapshot: snapshot)
                                outcome.append(info)
                            self.profInfo = outcome
                            self.tableView.reloadData()
                        }, withCancel: nil)
                }

   //tableview
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return profInfo.count
                }

                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

                    let cell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as! FollowingTableViewCell

                    cell.configCell(profInfo: profInfo[indexPath.row])

                    return cell
                }

然而,它返回一行,但profInfo实际上有两行。当我在print(self.profInfo)中实现fetchProf时,它会返回两个值。但是在传递给tableview之后,它变成了一个。我不确定,但我想原因是我将reloadData()置于错误的位置,因为我达到了断点并且reloadData()被调用了两次。所以,我认为profInfo被新值取代。我打电话到不同的地方,但没有工作。我对么?如果是这样,我应该在哪里拨打reloadData()?如果我错了,我该如何解决这个问题?提前谢谢!

4 个答案:

答案 0 :(得分:1)

必须从主队列中调用

self.tableView.reloadData()。试试

DispatchQueue.main.async {
    self.tableView.reloadData()
}

答案 1 :(得分:1)

您需要将新数据附加到profinfo数组。只需将fetchProf方法替换为: -

func fetchProf(key: String){     
         var outcome = [ProfInfo]()          
         ref.child("Profiles").child(key).observe(.value, with: {  (snapshot)   in    
         let info = ProfInfo(snapshot: snapshot)           
         outcome.append(info)     
         self.profInfo.append(contentOf: outcome)      
         Dispatch.main.async{
         self.tableView.reloadData()    
        }  
    } , withCancel: nil) 
}

答案 2 :(得分:1)

如果您注意到以下功能中的一件事,您将看到

func fetchProf(key: String){
                    var outcome = [ProfInfo]()
                        ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in
                                let info = ProfInfo(snapshot: snapshot)
                                outcome.append(info)

 //Here 
 /You are replacing value in self.profInfo
 //for the first time when this is called it results In First profile info
 //When you reload here first Profile will be shown 
 //Second time when it is called you again here replaced self.profInfo 
 //with second Outcome i.e TableView reloads and output shown is only second Profile
 //you had initialised a Array self.profInfo = [ProfInfo]()
 //But you just replacing array with Single value Actually you need to append data
 // I think here is main issue

 self.profInfo = outcome


 //So try Appending data as 
 //self.profInfo.append(outcome) instead of self.profInfo = outcome
 //Then reload TableView to get both outputs
 self.tableView.reloadData()

   }, withCancel: nil)
}

答案 3 :(得分:1)

显示一个内容的表视图,因为当重新加载表视图时,配置文件信息不会合并所有数据。您需要在组合所有数据后重新加载表视图。这对你有帮助。

// fetch 
        var profInfo = [ProfInfo]()

        func fetchUid(){
                guard let uid = Auth.auth().currentUser?.uid else{ return }
                ref.child("following").child(uid).observe(.value, with: { (snapshot) in
                    guard let snap = snapshot.value as? [String:Any] else { return }
                    snap.forEach({ (key,_) in
                        self.fetchProf(key: key)
                    })
                  // When all key fetched completed the just reload the table view in the Main queue 
                   Dispatch.main.async{
                       self.tableView.reloadData()    
                     }

                }, withCancel: nil)
            }

            func fetchProf(key: String){
                    ref.child("Profiles").child(key).observe(.value, with: { (snapshot) in
                        let info = ProfInfo(snapshot: snapshot)
                        self.profInfo.append(info) // Here just add the outcome object to profileinfo
                    }, withCancel: nil)
            }

这种方式无需处理另一个数组。