2016-09-14 23 views
0

在我的應用程序中,我試圖讓用戶能夠看到他們的帖子。現在,用戶只能看到他們最近的帖子,出於某種原因,並非所有的帖子都會經過並被添加到「myPosts」數組中。當我打印一個字典的「postDict」時,它給了我所有用戶所做的帖子的正確值,但出於某種原因,數據沒有通過並被添加到數組中。請幫助並參考代碼和我的JSON樹中的註釋。Xcode swift table view只顯示一個數據值而不是全部

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

回答

0

如果目標是顯示一個特定用戶的所有職位,你必須刪除此行self.myposts = []在您的帖子封處理,有一個嘗試。

相關問題