如何解决在 Swift Table View 中不填充 Firebase 数据
过去 2 天我一直被这个问题困扰,但找不到解决方案。我正在使用带有视图控制器的 Firebase,屏幕的顶部我用它来显示登录用户的信息,在我有一个表视图下,我想在其中显示其他人的图片和基本信息用户。我可以从 Firebase 下载数据并显示登录用户的信息。我遇到的问题是在单元格内填充数据。我创建了一个服务文件,因为我将在应用程序的其他区域多次使用这些功能。
这是我的代码:
import Foundation
import Firebase
struct Service {
static func fetchUser(withUid uid: String,completion: @escaping(Member) -> Void) {
COLLECTION_USERS.document(uid).getDocument { (snapshot,error) in
guard let dictionary = snapshot?.data() else { return }
let user = Member(dictionary: dictionary)
completion(user)}
}
static func fetchUsers(forCurrentUser user: Member,completion: @escaping([Member]) -> Void) {
var members = [Member]()
let query = COLLECTION_USERS
query.getDocuments { (snapshot,error) in
guard let snapshot = snapshot else { return }
snapshot.documents.forEach({ document in
let dictionary = document.data()
let user = Member(dictionary: dictionary)
guard user.uid != Auth.auth().currentUser?.uid else { return }
members.append(user)
if members.count == snapshot.documents.count - 1 { completion(members)
print("FROM SERVICE MEMBERS: \(members)") // Console displays this: FROM SERVICE FETCHING USERS COUNT 5
}}
)}
}
}
查看控制器
import UIKit
import Firebase
class HomeVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var memberFeedTableView: UITableView!
@IBOutlet weak var profileImage: userImagesView!
@IBOutlet weak var profileNameAgeLbl: UILabel!
@IBOutlet weak var profileCityLbl: UILabel!
@IBOutlet weak var profileStatus: UILabel!
@IBOutlet weak var changeFilterOptions: UIBarButtonItem!
var members = [Member]()
var userId = Auth.auth().currentUser?.uid
var handle: AuthStateDidchangelistenerHandle?
var userListener: ListenerRegistration!
override func viewDidLoad() {
super.viewDidLoad()
memberFeedTableView.dataSource = self
memberFeedTableView.delegate = self
memberFeedTableView.rowHeight = 450
configureUI()
fetchUserData()
}
func configureUI() {
navigationController?.navigationBar.isHidden = false
overrideUserInterfaceStyle = .light
tapToReleaseKeyboard()
let background = backgroundGradientColor()
background.frame = self.view.bounds
self.view.layer.insertSublayer(background,at: 0)
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return members.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = memberFeedTableView.dequeueReusableCell(withIdentifier: "MembersFeedCell",for: indexPath) as! MembersFeedCell
cell.configureMemberFeedCell(member: members[indexPath.row])
return cell
}
func fetchUsersData(forCurrentUser user: Member) {
Service.fetchUsers(forCurrentUser: user) { users in
self.members = users //issue is here,I'm not sure how to send the array to the table view to populate the cells.
print("CHECKING ARRAY..: \(self.members.count)") // Console displays this: CHECKING ARRAY..: 4
}
}
func fetchUserData() {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
Service.fetchUser(withUid: currentUid) { user in
self.fetchUsersData(forCurrentUser: user)
self.profileNameAgeLbl.text = user.name + "," + " " + (user.age)
self.profileCityLbl.text = user.city
self.profileImage.loadImage(with: user.imageURL)}
}
非常感谢任何帮助。谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。