Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 点击特定行时如何从Firestore获取特定信息_Swift_Firebase_Uitableview_Google Cloud Firestore - Fatal编程技术网

Swift 点击特定行时如何从Firestore获取特定信息

Swift 点击特定行时如何从Firestore获取特定信息,swift,firebase,uitableview,google-cloud-firestore,Swift,Firebase,Uitableview,Google Cloud Firestore,这里的目标是在Xcode的表视图中点击特定行时从Firebase Firestore获取特定信息。我已经找到了将数据存储到表视图的方法: func loadData() { FirebaseFirestore.root.collection("users").getDocuments { (snapshot, error) in if let error = error { print(error.localizedDesc

这里的目标是在Xcode的表视图中点击特定行时从Firebase Firestore获取特定信息。我已经找到了将数据存储到表视图的方法:

func loadData() {
        FirebaseFirestore.root.collection("users").getDocuments { (snapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                if let snapshot = snapshot {

                    for document in snapshot.documents {

                        let data = document.data()
                        let name = data["name"] as? String ?? ""
                        let email = data["email"] as? String ?? ""
                        let phone = data["phone"] as? String ?? ""

                        let newPerson = Person(name: name, email: email, phone: phone)
                        self.people.append(newPerson)
                        print (self.people)

                        let newIndexPath = IndexPath(row: self.people.count - 1, section: 0)

                        self.table.beginUpdates()
                        self.table.insertRows(at: [newIndexPath], with: .automatic)
                        self.table.endUpdates()
                    }
                }
            }
        }
    }
运行时,会显示以下内容:

例如,当我点击第1行时,我想进入另一个视图,该视图显示每个特定行的名称、电子邮件和电话。这可能吗?如果是这样,我该怎么做

------------------------------------

到目前为止,我想在每一行中添加另一个变量,但不在行上显示。因为在Firebase中,文档id是电子邮件,所以我可以将var email设置为每一行,并使用getDocument()通过电子邮件检索文档,即文档id

我所做的只是在didSelectRowAt函数中使用变量email来尝试使用getDocument

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let tappedUser = FirebaseFirestore.root.collection("users").document(email)

        performSegue(withIdentifier: "toPersonDesc", sender: nil)
    }
但这不起作用。我认为我不能使用email var,因为它为Person.email和email提供了一个错误

任何帮助都将不胜感激

-------------------------------

如果有人需要整个代码块(更新到我尝试过的版本):


嘿,这其实并不难,幸运的是。您需要做的就是这样:

//Create a custom UIViewController class which has a property of type Person.
class CustomViewController : UIViewController {    
    var person: Person?
    // You'll need to set this up using a .xib file
    weak var customView:CustomView?     
}

class UsersViewController : UIViewController, UITableViewDelegate {    
    var person:[Person]?

    //This method will be executed every time the user clicks on the row. Make sure that your current class adopts the UITableViewDelegate protocol.
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //Insert your functionality to get the correct person and set the person property in the new custom view controller.
        let viewController = CustomViewController()
        viewController.person = self.person?[indexPath.row]
        // Display the new custom view controller.
        self.navigationController?.pushViewController(viewController, animated: true)
    }
}
说明:

  • 创建具有Person类型属性的自定义UIViewController类
  • 每次用户单击行时,都将执行此方法func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath)确保当前类采用UITableViewDelegate协议。
  • 在didSelectRow函数中,插入您的功能以获取正确的person,并在新的自定义视图控制器中设置person属性
  • 显示新的自定义视图控制器
  • 您需要确保将视图控制器的视图设置为显示person对象中的数据。如果您需要了解如何做到这一点,请查看此信息。

    如果你想对此有任何澄清,请告诉我

    编辑:

    有多种方法可以做到这一点。Firebase具有查看文档的功能,如果文档服务器端有更改,您可以在应用程序中应用这些更改。看看吧

    我可能会考虑使用类似的东西


    或者,您可以在didSelectRow中获取数据,然后在检索到数据后显示新的视图控制器。

    有什么问题?你试过什么?@Daniel,哦,对不起,我还没有用我试过的内容编辑这个问题,我现在就编辑它。查看下面的答案。当用户点击发送该行电子邮件到下一个屏幕时。在下一个屏幕上,使用电子邮件查询数据库,并获取您需要的所有数据。@JawadAhmed,我如何将“电子邮件”发送到下一个屏幕?一旦我明白了,我想我可以完成剩下的。嗨,谢谢你的回答,但是我是使用故事板还是使用xib文件?我有点困惑。此外,我希望看到在点击一行时如何提取数据。例如,当我点击第一行时,我会从Firestore获取第一行的额外数据,并将其输入到另一个视图中。您知道如何执行此操作吗?在prepareForSegue方法中,在segue.destinationViewController对象上设置person对象。然后在viewDidLoad方法中,您可以运行代码从Firebase获取额外数据。但您需要Firebase提供哪些额外数据?一旦我得到这些信息,我可以提供更详细的解释。我正在根据你的位置创建一个应用程序,所以我需要邮政编码。然后,当我点击一行时,它会显示姓名、电子邮件、电话和邮政编码。另外,作为一个附带问题,您知道如何将电子邮件var转换为公共var吗?一个简单的说明可能会解决这个问题,即您是否拥有第一个表视图中的所有数据,或者只是邮政编码列表?无论如何,这本来自苹果的《快速入门指南》将帮助您解决您的问题(关于在ViewController之间传递数据的部分)@existingperson我刚刚更新了我的答案,也许会有所帮助。此外,要公开email var,您需要将其声明移到闭包之外的类级别。也许这篇文章可以帮助你。或者你可能想在谷歌上搜索Swift中的变量。
    //Create a custom UIViewController class which has a property of type Person.
    class CustomViewController : UIViewController {    
        var person: Person?
        // You'll need to set this up using a .xib file
        weak var customView:CustomView?     
    }
    
    class UsersViewController : UIViewController, UITableViewDelegate {    
        var person:[Person]?
    
        //This method will be executed every time the user clicks on the row. Make sure that your current class adopts the UITableViewDelegate protocol.
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            //Insert your functionality to get the correct person and set the person property in the new custom view controller.
            let viewController = CustomViewController()
            viewController.person = self.person?[indexPath.row]
            // Display the new custom view controller.
            self.navigationController?.pushViewController(viewController, animated: true)
        }
    }