在我的应用程序中,我试图让用户能够看到他们的帖子。现在,用户只能看到他们最近的帖子,并且由于某种原因,并非所有帖子都会经过并添加到“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()
}
}
最佳答案
如果目标是显示某个特定用户的所有帖子,则必须在帖子处理程序闭包中删除此行 self.myposts = []
,请尝试一下。
关于arrays - Xcode swift TableView 仅显示一个数据值而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39480961/