2017-03-27 2 views
2

でクリックしたセルのFirebaseデータベースキーを取得することはできません。この方法ではは、私は、Androidと経験を持っているとiOSへのいずれかの方法で変換するように期待していたUICollectionView

viewHolder.mView.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Log.v("TAG", model.toString()); 
         Log.v("TAG","ONCLICKED"); 
         Intent toPoll = new Intent(getActivity(), PollHostActivity.class); 
         //TODO: Go back to clicked item when navigating from Poll Fragment back to LiveFragment 
         toPoll.putExtra("POLL_ID", mFireAdapter.getRef(viewHolder.getAdapterPosition()).getKey()); 
         startActivity(toPoll); 
       } 
       }); 

を、私はFirebaseキーにアクセスできるようにしていますクリックしたアイテムの次のアクティビティに渡します。 iOS(Swift)では、キーを取得して新しいViewControllerに渡したいと思います。 UICollectionViewデリゲートメソッドと関係があることが分かっているので、少し混乱します。ここに私のコードは次のとおりです。

import UIKit 
    import FirebaseDatabase 
    import FirebaseDatabaseUI 
    import FirebaseStorageUI 
    import Material 


private let reuseIdentifier = "PollCell" 

class TrendingViewController: UICollectionViewController { 

    var ref: FIRDatabaseReference! 
    var dataSource: FUICollectionViewDataSource! 
    fileprivate var menuButton: IconButton! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     ref = FIRDatabase.database().reference() 
     prepareMenuButton() 


     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Register cell classes 

     self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell 
      /* populate cell */ 
      cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String? 
      let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String? 
      cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png")) 
      //Comment 
      return cell 
     } 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print(indexPath) 
} 


extension TrendingViewController { 
    fileprivate func prepareMenuButton() { 
     menuButton = IconButton(image: Icon.cm.menu) 
    } 
} 

EDIT:FirebaseUIフォーラムに投稿した後、私は私のシナリオに適用する方法を、私は次のコード行でレイヤーする必要があると判断することができた?:

- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index; 

答えて

1

didSelectItemAtデリゲートメソッドでは、次を実装します。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let selectedSnapshot = self.dataSource.items[indexPath.row])) 
    print(selectedSnapshot.key) 
} 

これは、選択したセルインデックスのFirebaseUIデータソースからスナップを引き出します。

関連する問題