在正确的地方调用reloadData()

我试图从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()?如果我错了,我该如何解决这个问题?先谢谢你!

回答:

您需要将新数据附加到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)

}

回答:

self.tableView.reloadData()必须从主队列中调用。尝试

DispatchQueue.main.async { 

self.tableView.reloadData()

}

回答:

如果您发现下面的函数一件事,你会看到

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)

}

回答:

显示,因为当表视图重载那么个人资料信息不合并一个内容表视图所有数据。合并所有数据后,您需要重新加载表格视图。这会帮助你。

// 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)

}

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

以上是 在正确的地方调用reloadData() 的全部内容, 来源链接: utcz.com/qa/266060.html

回到顶部