我正在创建一个虚拟的社交媒体应用程序,但UITableView
的行为并不是我所期望的。数据检索操作后,UITableView
未显示提取的内容。下面是我的FeedViewController
的代码:
import UIKit
import FirebaseFirestore
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var postArray = [[String: Any]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
getDataFromFirestore(){
print("self.tableView.reloadData() called from viewDidLoad()")
self.tableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
print("self.tableView.reloadData() called from viewDidAppear()")
self.tableView.reloadData()
}
func getDataFromFirestore(completion: @escaping() -> Void){
let ref = Firestore.firestore().collection("posts")
ref.addSnapshotListener { snapshot, error in
if error != nil {
self.makeAlert(title: "Error", message: error?.localizedDescription ?? "An error occoured during the process.")
} else {
if !(snapshot?.isEmpty ?? true) {
for doc in snapshot!.documents {
var dict = [String: Any]()
dict["userID"] = doc.get("userID") as? String
dict["comment"] = doc.get("comment") as? String
dict["imageURL"] = doc.get("imageURL") as? String
dict["likes"] = doc.get("likes") as? String
self.postArray.append(dict)
}
}
print("self.postArray.count been accessed from getDataFromFirestore(): \(self.postArray.count)")
}
}
completion()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedCell
cell.lblUserEmail.text = self.postArray[indexPath.row]["userID"] as! String? ?? ""
cell.lblLikes.text = self.postArray[indexPath.row]["likes"] as! String? ?? ""
cell.lblUserComment.text = self.postArray[indexPath.row]["comment"] as! String? ?? ""
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("self.postArray.count been accessed from numberOfRowsInSection: \(self.postArray.count)")
return postArray.count
}
func makeAlert(title: String, message: String) {
let controller = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "OK", style: UIAlertAction.Style.default)
controller.addAction(action)
self.present(controller, animated: true)
}
}
字符串
控制台输出:
self.tableView.reloadData() called from viewDidLoad()
self.postArray.count been accessed from numberOfRowsInSection: 0
self.postArray.count been accessed from numberOfRowsInSection: 0
self.postArray.count been accessed from numberOfRowsInSection: 0
self.tableView.reloadData() called from viewDidAppear()
self.postArray.count been accessed from numberOfRowsInSection: 0
self.postArray.count been accessed from getDataFromFirestore(): 1
型
下面是我的UITableViewCell
的代码:
import UIKit
class FeedCell: UITableViewCell {
@IBOutlet weak var lblUserComment: UILabel!
@IBOutlet weak var lblUserEmail: UILabel!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var lblLikes: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func btnLikeClicked(_ sender: Any) {
}
}
型
Firestore数据:
屏幕输出(空白,无数据):
的
1条答案
按热度按时间guykilcj1#
你在监听器外调用完成。因此完成将不会等待响应和重新加载TableView。
字符串
就这样做,享受吧:)