ios - Swift 数组未附加到

标签 ios arrays swift uitableview

我正在尝试获取一个 TableView 来显示已解析的数据,但是没有附加应该填充我的 TableView 的数组。 有人可以快速看一下,让我知道我做错了什么吗?

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)
}


}

谢谢你们。

最佳答案

您的数据检索似乎是一个异步操作,可能不会在表格呈现之前完成。尝试在 self.posts.append(post) 之后或完成 block 末尾的新行上重新加载表数据。

关于ios - Swift 数组未附加到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230566/

相关文章:

ios - WatchKit 调用的定位服务在后台不工作

javascript - 有没有办法从两个不同的数组中删除相同的值?

arrays - Segue 不传输数组

ios - 通过 segue 将 CKAsset 图像传递给 ViewController?

iphone - xcode 构建 svn 号插入不起作用

ios - UIProgressView 未反射(reflect)前 0.05 的进度

ios - 不应用表格 View 单元格高度

ios - 无法访问自定义单元格中的标签

ios - iOS 中 NSURLSession 使用后台线程或后台传输服务有很大区别吗

C编程 : reate 10 Element Array, 冒泡排序算法,返回数组的最小值和最大值