gpt4 book ai didi

ios - 正确放置 reloadData

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:06 26 4
gpt4 key购买 nike

所以我有一个 tableView,其中有一个标题元素和一些单元格。我正在从服务器中提取单元格,然后在完成后重新加载 tableview。好吧,这至少是现在的目标我在每次追加到数组后重新加载 tavleView。我最终只想在所有数据基本上被拉取一次后才重新加载它。功能

self.fetchEventsFromServer()

处理拉取数据的工作。我阅读了 dispatchGroups 并认为这是正确的方法,但我不知道该怎么做。

import UIKit
import Firebase

class FriendsEventsView: UITableViewController{
var cellID = "cellID"
var friends = [Friend]()
var followingUsers = [String]()
//label that will be displayed if there are no events
var currentUserName: String?
var currentUserPic: String?
var currentEventKey: String?

override func viewDidLoad() {
super.viewDidLoad()
self.title = "Friends Events"
view.backgroundColor = .white
// Auto resizing the height of the cell
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "close_black").withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(self.goBack))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
fetchEventsFromServer { (error) in
if error != nil {
print(error)
return
} else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

}

@objc func goBack(){
dismiss(animated: true)
}
override func numberOfSections(in tableView: UITableView) -> Int {
print(friends.count)
return friends.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return friends[section].collapsed ? 0 : friends[section].items.count
return 1
}
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
cell.textLabel?.text = "Something to fill Section: \(indexPath.section) Row: \(indexPath.row)"
return cell
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
print(section)
header.arrowLabel.text = ">"
header.setCollapsed(friends[section].collapsed!)
print(friends[section].collapsed!)
header.section = section
// header.delegate = self
header.friendDetails = friends[section]
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}

func fetchEventsFromServer(_ completion: @escaping (_ error: Error?) -> Void ){
//will grab the uid of the current user

guard let myUserId = Auth.auth().currentUser?.uid else {
return
}
let ref = Database.database().reference()
//checking database for users that the current user is following
ref.child("following").child(myUserId).observeSingleEvent(of: .value, with: { (followingSnapshot) in
//handling potentail nil or error cases
guard let following = followingSnapshot.children.allObjects as? [DataSnapshot]
else {return}

//validating if proper data was pulled
for followingId in following {
print(followingId.key)
ref.child("users").child(followingId.key).observeSingleEvent(of: .value, with: { (userInfoSnapShot) in
guard let followingUserInfo = userInfoSnapShot.children.allObjects as? [DataSnapshot] else {
return
}
//validating if proper data was pulled for each follower
for currentUserInfo in followingUserInfo {
if currentUserInfo.key == "username"{
self.currentUserName = currentUserInfo.value as! String
print(self.currentUserName)
var friend = Friend(friendName: self.currentUserName!, imageUrl: self.currentUserPic!)
self.friends.append(friend)
}
if currentUserInfo.key == "profilePic"{
self.currentUserPic = currentUserInfo.value as! String
print(self.currentUserPic)
}



}

}, withCancel: { (err) in
completion(err)
print("Couldn't grab info for the current list of users: \(err)")
})
completion(nil)
}

}) { (err) in
completion(err)
print("Couldn't grab people that you are currently following: \(err)")
}
completion(nil)

}
}

关于如何快速完成此任务的任何想法都让我很烦恼

Snapshot of following strucutre 
"following" : {
"Iwr3EWqFBmS6kYRjuLW0Pw0CRJw2" : {
"CW1AIDxM43Ot3C1GtsyhQ0Zzwof2" : true
},
"OYWgNjYHEtX6EatkolPO5YXt6Rw2" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true,
"qYSDao0zhFbLrzd0IJBafi7qdis2" : true
},
"nRrGzLFt3TeN4OOrwTe0RjHQoF13" : {
"CW1AIDxM43Ot3C1GtsyhQ0Zzwof2" : true,
"XV62sIs7anaGaoo0Wr9kooC8FDP2" : true,
"r51UQXn4Q2WcPWIhXIG3dhZTHkX2" : true
},
"nbmheFEPmBerm5avZwnriGJkaK12" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true
},
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : {
"OYWgNjYHEtX6EatkolPO5YXt6Rw2" : true,
"qYSDao0zhFbLrzd0IJBafi7qdis2" : true
},
"qYSDao0zhFbLrzd0IJBafi7qdis2" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true
},
"wdciX8B2LeUy2NyDwU5cjLog5xx2" : {
"72297UgQllfrEaAQnUCPKuQMv933" : true,
"nbmheFEPmBerm5avZwnriGJkaK12" : true
}
}

最佳答案

您想向 func fetchEventsFromServer() 函数添加一个完成处理程序,并在完成时调用 reloadData()

func fetchEventsFromServer(_ completion: @escaping (_ error: Error?) -> Void ) {
.........
withCancel: { (err) in
completion(err)
print("Couldn't grab info for the current list of users: \(err)")
})
} completion(nil) // Right here I would think
}) { (err) in
}

然后当你调用

fetchEventsFromServer { (error) in 
if error != nil {
print(error)
return
} else {
self.tableView.reloadData()
}
}

此外,您不需要将 self. 添加到代码中的许多内容中。只有当它嵌套在闭包之类的东西中时。

关于ios - 正确放置 reloadData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com