swift - 将文档引用传递给第二个 View Controller

标签 swift uitableview firebase segue google-cloud-firestore

有人可以告诉我如何做到这一点,以便当我单击 tableView 中的特定行时,将文档引用传递给第二个 ViewController,从而允许我显示子集合“Friends”中的字段。目前我可以做到这一点,但不使用 autoID。请有人告诉我如何使用 autoID 执行此操作?任何帮助将不胜感激,非常感谢!!

Current Firebase Console

What I would like - Firebase Console

第一个 View Controller

    func loadData() {
    db.collection("Users").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let data = document.data()
                let name = data["name"] as? String ?? ""
                let newName = UsersNames(name: name)
                self.nameArray.append(newName)
            }
            self.tableView.reloadData()
        }
    }
}

第二个 View Controller

    func loadData() {
    db.collection("Users").document("Hello").collection("Friends").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let data = document.data()
                let name = data["name"] as? String ?? ""
                let details = data["details"] as? String ?? ""
                let newFriends = Friends(friendName: name, friendDetails: details)
                self.friendsArray.append(newFriends)
            }
            self.tableView.reloadData()
        }
    }
}

最佳答案

如果你想那样做,首先你需要添加一个 documentID 属性到你的 UsersNames 对象:

    struct UsersNames {

        var documentID: String //<-- Add this
        var name: String
    }

然后更新您的第一个 VC 中的 loadData() 函数以从每个 Firestore 文档中获取 documentID 并将其附加到您的 数组:

        for document in querySnapshot!.documents {
            let data = document.data()
            let documentID = document.documentID //<-- Add this
            let name = data["name"] as? String ?? ""
            let newName = UsersNames(documentID: documentID, name: name) //<-- Change this
            self.nameArray.append(newName)
        }

在第一个 VC 中,您希望在选择单元格时对第二个 VC 执行 Segue,并为 中的所选对象传递 documentID数组到第二个VC。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "SecondVC", sender: self)
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let indexPath = tableView.indexPathForSelectedRow {
            let destinationVC = segue.destination as! SecondVC
            let documentId = nameArray[indexPath.row].documentID
            destinationVC.documentID = documentID
        }
    }

在您的 SecondVC 中创建一个属性来接收文档 ID:

    var documentID: String!

在您的 SecondVC loadData() 函数中,您现在可以访问从您的 First VC 传递的 documentID:

    db.collection("Users").document(documentID).collection("Friends").getDocuments() { //..

关于swift - 将文档引用传递给第二个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218462/

相关文章:

ios - Root View Controller Swift

android - 考虑为 Firebase 添加 .indexOn

ios - 成功登录后无法关闭 UIViewController

swift - 如何在 ARKit 2/SceneView 中扩展线段

ios - 当单元格不可见时停止异步 UITableViewCell 图像加载?

iOS-13 - UITableViewCell 的 CornerRadius

javascript - 云功能模拟器无法读取或写入 firestore 数据库

Swift 闭包和错误处理

ios - 在 Objective-C 文件中调用 Swift API

swift - 对于 iOS 10,每周特定的一天重复 UserNotification