2017-05-02 6 views
1

以前にテーブルビューから選択したユーザーの投稿を取得しようとすると、問題が発生します。名前やプロフィールなどのユーザー情報を取得することはできますが、投稿を取得するのが難しいです。現時点では、私のコードは何も返さない。私は下に私のコードを掲載しました。私が問題を抱えているのはで、ここでは関数loadUserPosts()を呼び出しています。どんな助けでも大変感謝しています。事前に感謝します!テーブルビューから選択されたユーザーの投稿を検索する方法

tableview controller 

@IBOutlet weak var searchTableView: UITableView! 
var profiles = [user]() 
var ref: FIRDatabaseReference! 

override func viewdidload(){ 
ref = FIRDatabase.database().reference() 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "guestSegue", sender: self) 
    self.searchTableView.deselectRow(at: indexPath as IndexPath, animated: true) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "guestSegue" { 

     if let indexpath = searchTableView.indexPathForSelectedRow { 

      let tabBarController = segue.destination as! UITabBarController 


      let guestVC = tabBarController.viewControllers![0] as! guestProfileViewController 


      _ = tabBarController.viewControllers![1] as! guestPhotoViewController 


      _ = tabBarController.viewControllers![2] as! guestVideoViewController 


      guestVC.ref = profiles[indexpath.row].ref 

       print(indexpath) 

      } 
     } 
    } 

guestPhotoViewController - 私はfirebase

class guestPhotoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 


    @IBOutlet weak var backgroundProfileImage: UIImageView! 

    @IBOutlet weak var profileImage: roundImage! 

    @IBOutlet weak var fullNameLabel: UILabel! 

    @IBOutlet weak var postCell: UITableViewCell! 

    @IBOutlet weak var postTableView: UITableView! 


var ref: FIRDatabaseReference! 

var posts = [Post]() 



override func viewDidLoad() { 
    super.viewDidLoad() 


    loadUserPosts() 

    ref = FIRDatabase.database().reference() 

} 

func loadUserPosts(){ 

if let ref = ref { 


    let postsRef = ref.child("post") 


    postsRef.observe(.value, with: { (snapshot) in 

       for post in snapshot.children { 

        let post = Post(snapshot: post as! FIRDataSnapshot) 

        self.posts.append(post) 

       print(post) 
       self.postTableView.reloadData() 

      } 
     }) 
    } 

} 

ポスト配列から

class Post{ 
    var profilePic: String! 
    var fullName: String! 
    var postImage: String! 
    var postCaption: String! 
    var date: NSNumber! 

init(profilePic: String, fullName:String, postImage:String, postCaption: String, date:NSNumber){ 
    self.profilePic = profilePic 
    self.fullName = fullName 
    self.postImage = postImage 
    self.postCaption = postCaption 
    self.date = date 
} 

init(snapshot:FIRDataSnapshot) { 
    self.profilePic = (snapshot.value! as! NSDictionary)["profilePic"] as! String! 
    self.date = (snapshot.value! as! NSDictionary)["dateOfPost"] as! NSNumber! 
    self.fullName = (snapshot.value! as! NSDictionary)["fullName"] as! String! 
    self.postImage = (snapshot.value! as! NSDictionary)["postedPic"] as! String! 
    self.postCaption = (snapshot.value! as! NSDictionary)["postCaption"] as! String! 
} 

答えて

0

いくつかの記事を取得しようとしているところです。

1:あなたオーバーライドする場合それが適用される場合はsuper.methodに電話することを忘れないでください。

2:何が失敗するかは、またはのデータを表示するためのテーブルビューの更新であることはあまり明確ではありません。

また、 の前にという参照を設定しているようです。あなたは逆にそれをしてはいけませんか?最初に参照を設定してから要求を出しますか?そうしないと、if let ref = refは常に失敗します。

編集:新しい問題については、取得するデータをフィルタリングする方法についての問題と思われます。または、この場合、どのようにではなく、をフィルタリングします。

は、この(あなたのニーズに適応)返信@AleRavasioのためのあなたのguestPhotoViewController

class guestPhotoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    @IBOutlet weak var backgroundProfileImage: UIImageView! 
    @IBOutlet weak var profileImage: roundImage! 
    @IBOutlet weak var fullNameLabel: UILabel! 
    @IBOutlet weak var postCell: UITableViewCell! 
    @IBOutlet weak var postTableView: UITableView! 
    private var ref: FIRDatabaseReference! 
    private var posts = [Post]() 
    private var posterId: Int = -1 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     ref = FIRDatabase.database().reference() 
     loadUserPosts() 
    } 

    func prepareVC(withId: id) { 
     self.posterId = id 
    } 

    func loadUserPosts(){ 
     if self.id != -1 { 
      if let ref = ref { 
       /* Probably something around here is messed up. You don't seem to be filtering by id, just retrieving "post". 
        You might want to consider checking how you filter (you don't seem to do so at all) the postRef, since that seems to be the issue. 
        I don't know your FireBase models, so it's harder to help you with that. 
       */ 
       let postsRef = ref.child("post").equalTo(self.posterId) 
       /* 
       or if they are ordered, you can use: 
       let postsRef = ref.queryOrderedByChild("post").queryEqualToValue(self.posterId) 
       */ 
       postsRef.observe(.value, with: { (snapshot) in 
        for post in snapshot.children { 
         let post = Post(snapshot: post as! FIRDataSnapshot) 
         self.posts.append(post) 
         print(post) 
         self.postTableView.reloadData() 
        } 
       }) 
      } 
     } 
    } 
} 
+0

のおかげで、あなたのtableViewVC

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let vc = UIStoryboard(name: "Name", bundle: nil).instantiateViewController(withIdentifier: "vcIdentifier") as! YourViewController vc.prepareVC(withId: indexPath.row) self.navigationController?.pushViewController(vc, animated: true) } 

のようなものを試してみてください。あなたの権利私は間違ってrefUpdateの前にloadUserPostを呼びましたので、変更しましたが、今選択したユーザーの投稿だけでなく、すべてのユーザーの投稿を読み込むので問題があります。以前のテーブルビューから選択したユーザーの投稿のみを呼び出す方法でコードを作成する方法がわかりません。手伝っていただけませんか?私は他の情報を取得するために同様の呼び出しメソッドを使用し、それは働いた。私はコーディングの世界に新しいので、物事が意味をなさない場合は私を許します –

+0

私はあなたが書いたもので私のコードを更新しようとしましたが、私は幸運を得ていないよ。私はインデックスでエラーが発生しています。また、prepareVC initでエラーが発生しています。私は自分の投稿配列に参照を追加しましたが、それでも正しい投稿を検索しようとすると困っています。 Firebaseでは、私はそれを「投稿」として設定しました。子供(userID).child(postID)それがあなたがそれをより良く理解するのを助けるなら@AleRavasio –

関連する問題