【问题标题】:Xcode swift table view only showing one data value and not allXcode swift table view 只显示一个数据值而不是全部
【发布时间】:2017-01-21 16:48:29
【问题描述】:

在我的应用程序中,我试图让用户能够看到他们的帖子。现在用户只能看到他们最近的帖子,并且由于某种原因,并不是所有的帖子都通过并被添加到“myPosts”数组中。当我打印作为字典的“postDict”时,它为我提供了用户发布的帖子的所有正确值,但出于一个原因,数据没有通过并被添加到数组中。请帮助并参考代码中的 cmets 和我的 JSON 树。

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

【问题讨论】:

    标签: arrays json swift firebase firebase-realtime-database


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多