[英]Swift Array not being appended to
我正在嘗試獲取一個表視圖以顯示已解析的數據,但是沒有追加應該填充我的表視圖的數組。 有人可以快速瀏覽一下,讓我知道我在做什么錯嗎?
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)
或在完成代碼塊的末尾在新行上重新加載表數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.