解析されたデータを表示するためにtableviewを取得しようとしていますが、テーブルビューを作成しようとしている配列が追加されていません。 誰かがすばやく見て、私が間違っていることを教えてもらえますか?Swift Arrayが追加されていません
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をクロージャーに入れて、クロージャが終了した後にテーブルビューを再ロードするようにしてください。 – Akaino
forループの後に 'tableView.reloadData()'が終了しました –
...しかし、 'observeEventType'の完了ハンドラの中です。 – Rob