我试图让tableview显示已解析的数据,但应该填充我的tableview的数组不会被追加。 有人可以快速查看,让我知道我在做什么错了吗?Swift数组不被追加到
import UIKit
import SwiftKeychainWrapper
import Firebase
class FeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var posts = [Post]()
override func viewDidLoad()
{
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
DataService.ds.REF_POSTS.observeEventType(.Value, withBlock: { snapshot in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
{
for snap in snapshot
{
if let postDict = snap.value as? Dictionary<String, AnyObject>
{
let key = snap.key
let post = Post(postID: key, postData: postDict)
self.posts.append(post)
}
}
}
})
tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
print("\(post.caption)")
return tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostCell
}
@IBAction func signOutBtnTapped(sender: UIButton) {
KeychainWrapper.defaultKeychainWrapper().removeObjectForKey(KEY_UID)
try! FIRAuth.auth()?.signOut()
performSegueWithIdentifier("toSignInVC", sender: nil)
}
}
谢谢你们。
你确定你的阵列没有被填充?尝试在封闭内打印它。如果它打印一个填充的值,将reloadData放入闭包中,以确保在闭包结束后重新加载tableView。 – Akaino
for循环结束后do'tableView.reloadData()'结束 –
...但在'observeEventType'的完成处理程序中。 – Rob