我能够从Firebase成功下载数据,但是似乎无法及时下载UITableView才能使用它。
我尝试将代码放入viewDidLoad()以及各种UITableView函数中,但无济于事。
//
// PostsViewController.swift
// cheerup-ios
//
// Created by Diamonique Danner on 12/26/18.
// Copyright © 2018 Danner Opp., LLC. All rights reserved.
//
import UIKit
import Firebase
class PostsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var postDictionary : NSDictionary!
var currentPost : String!
var postData = [Any]()
@IBOutlet var postsTable: UITableView!
@IBOutlet var basicPostsTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
postsTable.dataSource = self
postsTable.delegate = self
let dbRef = Database.database().reference()
dbRef.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let posts = value!["posts"] as? NSDictionary
self.postDictionary = posts!
for post in posts! {
self.postData.append(post)
// print((post.value as AnyObject)["photoURL"]!!)
// print((post.value as AnyObject)["post"]!!)
}
print(self.postData)
// self.currentPostDictionary = self.postData[0] as? NSDictionary
// self.currentPost = self.currentPostDictionary.value as! String
// print(self.postData)
}) { (error) in
print(error.localizedDescription)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(self.postDictionary.count)
return self.postDictionary.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.postsTable.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
// cell.postText.text = String(indexPath.row)
let data = postData[indexPath.row]
return cell
}
}
我希望postData能够在cellForRowAt中用于用数据填充表,并且还可以在numberOfRowsInSection中使用,因为它当前表示数组上有零个元素,而实际上我身上只有三个元素”打印出来。