在我的应用中,我试图让用户能够看到他们的帖子。现在,用户只能看到他们最近发布的帖子,并且出于某种原因并非所有帖子都经过并被添加到“myPosts”数组中。当我打印“postDict”这是一个字典时,它给了我用户所做的所有帖子的所有正确值,但由于一个原因,数据没有通过并被添加到数组中。请帮助并参考代码和我的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()
}
}
答案 0 :(得分:0)
如果目标是显示一个特定用户的所有帖子,则必须在帖子处理程序关闭中删除此行self.myposts = []
,请尝试。