我试图从firebase中获取数据并传递给tableview。在正确的地方调用reloadData()
// 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
实际上有两行。当我在fetchProf
里执行print(self.profInfo)
时,它返回两个值。但传递给tableview后,它成为一个。我不知道,但我想是因为我把reloadData()
放在错误的地方,因为我打中断点和reloadData()
调用两次。所以,我认为profInfo
取代新的价值。我在不同的地方打电话,但没有工作。我对么?如果是这样,我应该在哪里拨打reloadData()
?如果我错了,我该如何解决这个问题?先谢谢你!
当reloadData被调用两次时,print(self.profInfo)在每种情况下都会打印什么? –