arrays - Xcode swift TableView 仅显示一个数据值而不是全部

标签 arrays json swift firebase firebase-realtime-database

在我的应用程序中,我试图让用户能够看到他们的帖子。现在,用户只能看到他们最近的帖子,并且由于某种原因,并非所有帖子都会经过并添加到“myPosts”数组中。当我打印“postDict”(这是一本字典)时,它会为我提供用户发布的帖子的所有正确值,但由于一个原因,数据不会通过并添加到数组中。请帮助并引用代码中的注释和我的 JSON Tree。

import UIKit
import Firebase

class MockPostVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var mockPost: MockPost!

var myposts = [MockPost]()
var postKey: String?

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    DataSevice.ds.REF_USER_CURRENT.child("posts").observeEventType(.Value, withBlock:  { snapshot in

        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

            for snap in snapshots{

                var y = "\(snap)"
                var x = y.componentsSeparatedByString("Snap (")
                var z = x[1].componentsSeparatedByString(") 1")

                DataSevice.ds.REF_POSTS.observeEventType(.Value, withBlock: { snapshot in

                    self.myposts = []
                    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                        for snap in snapshots{

                            if "\(z[0])" != snap.key{
                            }
                            else{
                                if let postDict = snap.value as! Dictionary<String, AnyObject>?{

                                    self.postKey = snap.key
                                    let mockPost = MockPost(dictionary: postDict, postKey: snap.key)
                                    self.myposts.append(mockPost)
                                    //self.myposts.count returns as 1
                                    //for some reason only the first post is getting registered into the array
                                }
                            }
                        }
                    }
                    self.tableView.reloadData()
                })
            }
        }
    })
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myposts.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let mockPost = myposts[indexPath.row]
    if let cell = tableView.dequeueReusableCellWithIdentifier("MockPostCell") as? MockPostCell {
        cell.request?.cancel()
        cell.configureCell(mockPost, postKey: postKey!)

        return cell
    } else {
        return MockPostCell()
    }
}

enter image description here

最佳答案

如果目标是显示某个特定用户的所有帖子,则必须在帖子处理程序闭包中删除此行 self.myposts = [],请尝试一下。

关于arrays - Xcode swift TableView 仅显示一个数据值而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39480961/

相关文章:

php 以更快的方式将具有值的键添加到数组

android - 阻止非 Android 用户调用 JSON 服务

ios - 更改 NSLayoutConstraint 的常量

json - Swift - 将图像从 URL 写入本地文件

arrays - O(nlog(n)) 中最小的下一个更大的元素

python - 使用 NumPy 查找条件为 True 的范围

java - 对适应度函数数组进行排序

python - 返回属性错误 : 'int' object has no attribute 'encode'

javascript - AngularJS(前端)和 Laravel(后端)中 JSON/Arrays 中的 NULL

ios - Swift UITabBarController 隐藏动画