gpt4 book ai didi

ios - 根据项目名称和详细信息从firebase中检索用户的信息-Swift项目

转载 作者:行者123 更新时间:2023-11-29 01:04:11 25 4
gpt4 key购买 nike

这是使用 swift 和 firebase 的 iOS 应用程序的一些用户的仪表板结构:

{
"users" : {
"0a0462bb-86a1-4140-bda2-90a62236ab1e" : {
"Picker" : "Jeddah",
"details" : "I'm here",
"email" : "y@gmail.com",
"name" : "y",
},
"0b71693a-4f16-4759-acd6-706da1a466a0" : {
"Picker" : "Jubail",
"details" : "iOS second",
"email" : "Siham0@gmail.com",
"name" : "Siham",
},
"0b71693a-4f16-4759-acd6-706da1a466a0" : {
"Picker" : "Jeddah",
"details" : "",
"email" : "roro0@gmail.com",
"name" : "roro",
}
}
}

如下图所示:

enter image description here

模拟器中的 TableView 包含选择器为 Jeddah 的所有用户的姓名和详细信息。

我现在想要的是在用户单击任何单元格时在另一个 View Controller 中检索标签中的姓名、电子邮件和详细信息!

例如,如果我单击第一个单元格,第二个 View Controller 将具有以下内容:

姓名:y

详情:我在这里

电子邮件:y@gmail.com

我应该怎么做才能让它发挥作用?和我应该使用什么样的检索函数结构?

最佳答案

我确实通过简单的格式制作了一个包含所有列表值的数组,并显示如下所示的项目列表:

var namesArray : NSMutableArray = []

viewDidLaod 方法如下所示:

 override func viewDidLoad() {
super.viewDidLoad()



ref.queryOrderedByChild("Picker").queryEqualToValue("Makkah")
.observeSingleEventOfType(.Value, withBlock: { snapshot in


if let dict = snapshot.value as? NSMutableDictionary{
print("print \(dict)")


for (key,value) in dict {
let mainDict = NSMutableDictionary()
mainDict.setObject(key, forKey: "userid")


if let dictnew = value as? NSMutableDictionary{

if let metname = dictnew["name"] as? String
{
mainDict.setObject(metname, forKey: "name")

}
if let metname = dictnew["Picker"] as? String
{
mainDict.setObject(metname, forKey: "Picker")

}
if let metname = dictnew["details"] as? String
{
mainDict.setObject(metname, forKey: "details")

}
if let metname = dictnew["email"] as? String
{
mainDict.setObject(metname, forKey: "email")

}
if let metname = dictnew["provider"] as? String
{
mainDict.setObject(metname, forKey: "provider")

}

}
self.namesArray.addObject(mainDict)
}

print("arrayt is \(self.namesArray)")


}

self.CookingTableView.reloadData()
})


}

而它在 TableView 的 cellForRowAtIndexPath 中的数组 Show 必须有如下代码:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.CookingTableView.dequeueReusableCellWithIdentifier("CookingCell", forIndexPath: indexPath) as! CookingTableViewCell

if let name = namesArray[indexPath.row] as? NSMutableDictionary{

cell.BusinessNameLabel?.text = name["name"] as? String
cell.DescriptionLabel?.text = name["details"] as? String
}

cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

return cell

}

现在列表的事情已经完成了。让我们在 didSelectRowAtIndexPath 方法上显示其详细信息:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


tableView.deselectRowAtIndexPath(indexPath, animated: true)
dispatch_async(dispatch_get_main_queue()) { [unowned self] in

let DetailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ProjectDetailsViewController") as! ProjectDetailsViewController

if let name = self.namesArray[indexPath.row] as? NSMutableDictionary{

DetailsViewController.self.strUserid = name["userid"] as? String
}


self.navigationController?.pushViewController(DetailsViewController, animated: true)

}
}

在上面的 didSelect 方法中,我传递了唯一用户 ID 所需的 userID,以便在 PushedViewController 中获取其详细信息。您还可以将所选项目的墙字典发送到 nextviewController,但我在 nextViewController 中使用调用的 firebase 查询方法,因此 nextViewController 代码如下所示:

var strUserid : NSString!

这是 ViewDidLoad:

 override func viewDidLoad() {
super.viewDidLoad()

print("idis \(self.strUserid)")


ref.childByAppendingPath(self.strUserid as String).observeEventType(.Value, withBlock: { snapshot in


if let dict = snapshot.value as? NSMutableDictionary{

print("dict is \(dict)")

if let name = dict["name"] as? String
{
self.Name.text = name

}

if let details = dict["details"] as? String
{
self.Details.text = details

}

if let email = dict["email"] as? String
{
self.Email.text = email

}

}



})
}

就是这样希望您的问题能够得到解决。我用上面的方法做了这种类型的方法。

关于ios - 根据项目名称和详细信息从firebase中检索用户的信息-Swift项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com