无法获取UICollectionView中点击单元格的Firebase数据库密钥



我以前有过Android的经验,希望将其中一种方法翻译成iOS:

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)
  }
}

编辑:在FirebaseUI论坛上发布后,我能够确定我需要在以下代码行中分层,如何将其应用于我的场景?

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

didSelectItemAt 委托方法中实现以下内容。

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

这会从所选单元格索引处的 FirebaseUI 数据源中提取快照。

最新更新