Here's the dashboard structure with some of the users for an iOS application using swift and firebase:
{
"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",
}
}
}
As in the picture bellow:
The table view in the simulator contains the name and the details for all of the users whose picker is Jeddah.
What I want now is to retrieve the name, email and details in labels in another view controller whenever the user clicks at any cell!
For example, If I clicked on the first cell, the second view controller will have the following:
Name: y
Details:I'm here
Email: y@gmail.com
What should I do to make that work? And What's the structure of retrieving function should I use?
I did make a array of all listing value by easy format and display list of item like following:
var namesArray : NSMutableArray = []
And it's viewDidLaod
Method look like following:
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()
})
}
And its array Show in TableView's cellForRowAtIndexPath
must be have following code:
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
}
Now the listing thing is complete. Let display its details on didSelectRowAtIndexPath
Method:
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)
}
}
Here above didSelect method i Pass the userID
that unique user id need to get it's details in PushedViewController. You can also send selected items's wall dictionary to nextviewController but i do with called firebase query method in nextViewController so the nextViewController code look like:
var strUserid : NSString!
And it's 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
}
}
})
}
That's It Hope your issue going to be solve. I did this type of approach with above way.