Swift 点击特定行时如何从Firestore获取特定信息
这里的目标是在Xcode的表视图中点击特定行时从Firebase 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
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)
}
}
说明:
或者,您可以在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)
}
}