[英]Display custom information according to NavigationLink Text Swift, Cloud Firestore
I have this code to get fields "spty" with autoID from collection "specialities":我有这个代码可以从集合“specialities”中获取带有 autoID 的字段“spty”:
struct Spty: Identifiable{
var id: String = UUID().uuidString
var spty: String
}
class SptyViewModel: NSObject, ObservableObject{
@Published var specialities = [Spty]()
func fetchData(){
let db = Firestore.firestore()
db.collection("specialities").addSnapshotListener { (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {return }
self.specialities = documents.map { (queryDocumentSnapshot) -> Spty in
let data = queryDocumentSnapshot.data()
let spty = data["spty"] as? String ?? ""
return Spty(spty: spty)
}
}
}
}
They are displayed by using ForEach:它们使用 ForEach 显示:
@StateObject var sptyModel = SptyViewModel()
ForEach(sptyModel.specialities){ spty in
NavigationLink(destination: More()) {
Text(spty.spty).foregroundColor(.black)
As in the code, each Text(spty.spty)
is a NavigationLink, so what I want is to display different information according to the Text that I pressed.和代码一样,每个Text(spty.spty)
都是一个NavigationLink,所以我想要的是根据我按下的Text显示不同的信息。
Im currently working on this getDocuments:我目前正在处理这个 getDocuments:
struct Testtt: Identifiable{
var id: String = UUID().uuidString
var name: String
}
class TesteViewModel: NSObject,ObservableObject{
@StateObject var LocationModel = LocationViewModel()
let db = Firestore.firestore()
@Published var testsss = [Testtt]()
@Published var sptyModel = SptyViewModel()
@Published var SptyNameee = [Spty]()
func testeApp(){
db.collection("Test").whereField("Los Angeles", isEqualTo: true).getDocuments { (querySnapshot, err) in
guard let documents = querySnapshot?.documents else {return }
self.testsss = documents.map { (queryDocumentSnapshot) -> Testtt in
let data = queryDocumentSnapshot.data()
let name = data["Name"] as? String ?? ""
return Testtt(name: name)
}
}
}
}
.whereField
忽略.whereField
What I've been thinking is that to answer this question I need to relate collection()
in testeApp() with spty.spty
, but didn't managed to do it.我一直在想的是,要回答这个问题,我需要涉及collection()
在testeApp()与spty.spty
,但并没有成功地做到这一点。
Any ideas?有任何想法吗?
EDIT:编辑:
[![2 ][4]][4] [![2][4]][4]
To clarify my question I added screenshots on how Im currently managing the data:为了澄清我的问题,我添加了有关我目前如何管理数据的屏幕截图:
I get 3 NavLinks in Home View
accordion to collection "specialities" and the code given above: Cardiologista
, Cirurgião Geral
and Oftalmoloista
我得到了3个NavLinks Home View
手风琴集“特色”和上面给出的代码: Cardiologista
, Cirurgião Geral
和Oftalmoloista
Then, if I press any of the NavLinks, I got to More
View然后,如果我按任何一个 NavLinks,我就会进入More
视图
There, what I want is: if the user presses Cardiologista
, the name "Mateus Neves" will appear for him.在那里,我想要的是:如果用户按下Cardiologista
,他就会出现“Mateus Neves”这个名字。 But, I he presses Oftalmologista
, the name "Vitor Souza" will appear.但是,我按下Oftalmologista
,就会出现“Vitor Souza”这个名字。
I am going to attempt address a comment in the question first, which may lead to the answer.我将首先尝试解决问题中的评论,这可能会导致答案。
If you click “Top Paid Apps”, X apps will be displayed.如果您单击“热门付费应用”,将显示 X 个应用。 If you click on “Top Free Apps”, Y apps will be displayed如果您点击“Top Free Apps”,则会显示 Y 个应用程序
Here's a structure that supports that UI这是一个支持该 UI 的结构
allApps (a collection)
app_0 (a document)
app_type = "Free"
app_1
app_type = "Paid"
As you can see, if a user clicks on Free apps, a query is run against Firestore for all documents where app_type isEqual(to: "Free")
如您所见,如果用户单击免费应用程序,则会针对app_type isEqual(to: "Free")
为app_type isEqual(to: "Free")
所有文档对 Firestore 运行查询
So then let's expand on that.那么让我们扩展一下。 Lets take specialty types and store them in a collection - this would be the list presented to the user that can select from让我们采用专业类型并将它们存储在一个集合中 - 这将是呈现给用户的列表,可以从中进行选择
specialties
type_0
name: "Cardiologist"
type_1
name: "General"
type_2
name: "Anesthesiologist"
then the doctors would be stored in a separate collection那么医生将被存储在一个单独的集合中
doctors
doctor_0
name: "Dr. Smith"
type: "type_0"
doctor_1
name: "Dr. Jones"
type: "type_1"
doctor_2
name: "Dr. Jenkins"
type: "type_2"
So when the user clicks on Cardiologist in the list, you know that's type_0 and then query the doctors node for all documents where type isEqual(to: "type_0")
which returns Dr. Smith.因此,当用户单击列表中的 Cardiologist 时,您知道这是 type_0,然后查询医生节点以查找type isEqual(to: "type_0")
返回 Dr. Smith 的所有文档。
Note笔记
Make sure you map the documents into an object in code that will hold both the type (the documentId) as the name so you can use that as a tableView datasouce.确保将文档映射到代码中的对象中,该对象将同时包含类型(documentId)作为名称,以便您可以将其用作 tableView 数据源。
class MyType {
var type = "" //the firestore documentId
var name = ""
}
var myTableViewDataSourceArrayOfTypes = [MyType]() //the tableView datasource
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.