android - 无法获取 UICollectionView 中单击的单元格的 Firebase 数据库 key

标签 android ios swift firebase firebaseui

我以前有使用 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 键并将其传递到下一个 Activity 。在 iOS (Swift) 中,我想获取 key 并传递给新的 ViewController。这让我有点困惑,因为我知道它与 UICollectionView 委托(delegate)方法有关。这是我的代码:

    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 委托(delegate)方法中实现以下内容。

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

这将从所选单元格索引处的 FirebaseUI 数据源中拉出快照。

关于android - 无法获取 UICollectionView 中单击的单元格的 Firebase 数据库 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054518/

相关文章:

android - 即使在 Android 中使用 @hide 注解后,API 也不会被隐藏

java - Android - getType(int) 未定义 Cursor 类型

Android 从 intentservice 共享 intent

java - JAVA判断字符是否有大小写

ios - 使用 swift 从 JSON 检索数据时出现问题

ios - 如何从 Firebase 身份验证登录的 NSError 中获取错误消息

ios - 在 iOS 13 全屏中呈现模式

ios - 测试 VoiceOver : how can I verify that my UIAccessibilityLayoutChangedNotification notifications are working?

ios - iPhone X模拟器上的软件键盘

ios - 核心数据: Do we need to re-fetch after every delete?