tableViewを再ロードする際に問題があります。私のfirebaseService.getAllPosts()
メソッドが実行されると、データベースに2つの投稿が表示されます。私はdidSet
印刷が実行されるとき、それが私に正しいカウントを与えるので、これを言うことができます。しかし、メソッドが実行される前にtableviewが設定されていることがわかっているので、データソースのカウントを更新するためにtableviewをリロードする必要があります。そこに問題がある。私はposts
変数を私のクラスの外に入れて、すべてのクラスからアクセスできるようにしました。 (これは良い練習ではない場合、私に教えてください)テーブルの再読み込みに問題があります
私はtableView.reloadData()
を実行することができますので、私のtableViewデータソースの更新は、私にposts
の正しいことを教えてくれますか?私はFeedController().tableView.reloadData()
をdidSetに入れてみましたが、私はviewDidLoad()
に入れてみましたが、どちらもうまくいきませんでした。私はまた、_posts
という変数を追加し、posts
に等しい、それを設定し、didSet
内部tableView.reloadData()
とそれにdidSet
を追加しようとしたが、それはどちらか動作しません。
class FeedController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
let cellId = "PhotoCell"
let textCellId = "TextCell"
let firebaseService = FirebaseService.sharedInstance
static let sharedFeedInstance = FeedController()
var posts = [Post]() {
didSet {
tableView.reloadData()
print(posts.count)
}
}
override func viewDidLoad() {
super.viewDidLoad()
firebaseService.getAllPosts()
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
print("sections: \(posts.count)")
return posts.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post:Post? = posts[indexPath.section]
if let _ = post?.imageContentName {
let photoFeedCell = tableView.dequeueReusableCellWithIdentifier(self.cellId, forIndexPath: indexPath) as? FeedTVCellWithPhoto
photoFeedCell?.post = post
return photoFeedCell!
}
let textFeedCell = tableView.dequeueReusableCellWithIdentifier(self.textCellId, forIndexPath: indexPath) as? FeedTVCellText
textFeedCell?.post = post
return textFeedCell!
}
}
アップデート1:FirebaseServiceクラスからgetAllPosts方法
func getAllPosts() {
let postRef = ref.child("posts")
postRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
// print(snapshot.value)
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(key: key, dictionary: postDictionary)
FeedController.sharedFeedInstance.posts.insert(post, atIndex: 0)
}
}
}
})
}
問題は、画面上にあるビューコントローラのコンテキストで 'reloadData'を実行する必要があることです。 'FeedController()。tableView.reloadData()'は、 'FeedController'の新しいインスタンスを作成しますが、これは役に立ちません。あなたが望むものを得ることができる1つの方法は、 'didSet'からNSNotificationを投稿し、関心のあるビューコントローラにそれをサブスクライブさせることです。そして、はい、グローバル変数は悪い考えです。シングルトンクラスを作成するか、1つのインスタンスを作成して各ビューコントローラに渡します。 – Paulw11
'reloadData'を呼び出すには、' getAllPosts'がいつ終了するかを知る必要があります。 'getAllPosts'はどのように見えますか? – Cristik