0

Firebase Database

Firebase Database

driver1 Main page

driver1 Main page

toyota car details

toyota car details

FInal Output

enter image description here So the problem is that driver1 has 2 cars. how can i make the tableView show for toyota car information and mazda car information.

I was able to show driver 1's car list by this code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "driverRequestCell", for: indexPath)

if let email = Auth.auth().currentUser?.email {

Database.database().reference().child("Driver").queryOrdered(byChild: "email").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in

    let snapshot = self.driverRequests[indexPath.row]

    if let driverRequestDictionary = snapshot.value as? [String:AnyObject] {

    if let typeOfCar = driverRequestDictionary["car"] as? String {

            cell.textLabel?.text = typeOfCar

        }
    }
})
}
return cell

}

So my current code for didSelectRowAt is:

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

    let snapshot = driverRequests[indexPath.row]

    performSegue(withIdentifier: "carDetailsSegue", sender: snapshot)

}

I think it has something to do with the snapshot, but I can't figure it out. Need help from the pro's

Nick
  • 104
  • 11

1 Answers1

0

If you want to show all car information in the same cell, first you need a cell with three UILabel, one for all of the information you want to show, This is an example:

enter image description here

I suppose you have your own custom cell class, so you need to drag&drop label outlets into your class.

Now, you have to change you code into cellForRowAt: this way:

if let driverRequestDictionary = snapshot.value as? [String:AnyObject] {

if let typeOfCar = driverRequestDictionary["car"] as? String {
        cell.modelLabel?.text = typeOfCar
    }

if let colorOfCar = driverRequestDictionary["color"] as? String {
        cell.colorLabel?.text = colorOfCar
    }

if let plateOfCar = driverRequestDictionary["plate"] as? String {
        cell.plateLabel?.text = plateOfCar
    }
}

You can use a default value if color and plate doesn't exists adding else statements, or using three operand:

`if let plateOfCar = driverRequestDictionary["plate"] as? String {
        cell.plateLabel?.text = plateOfCar
    }else{
       cell.plateLabel?.text = "Unknown"
    }`

ADVICE: Avoid to do a request into cellForRowAt:, Instead of this, make an asynchronous request (on viewDidLoad: for example) using a closure. Then use the result of the closure to fill an array and then reload you table view.

EDIT:

You should struct your nodes this way, where giuseppesapienza is user ID, while note is your car objext:

enter image description here

Andrew21111
  • 868
  • 8
  • 17
  • hello Andrea, thanks for replying. the information shows but when i tap on the mazda car, the information for toyota still show. its like whenever driver1@gmail.com adds a car, the recent information shows even when the other cars are selected – Nick Sep 26 '18 at 14:05
  • im still using the code you wrote, i have a list of problems and you've answered one of them. thanks! – Nick Sep 26 '18 at 14:06
  • Hi Nick, please to hear this, but I don't understand, Do you actually have problems or are they all resolved ? – Andrew21111 Sep 26 '18 at 14:13
  • the problem is that when i press toyota or mazda, the information for toyota shows. – Nick Sep 26 '18 at 14:23
  • I'v added another photo so that it would be easier to understand. – Nick Sep 26 '18 at 14:30
  • Actually, the model definition is wrong. You have two users with different ID but they're actually the same user. You need another type of structure, I will show you a pic – Andrew21111 Sep 26 '18 at 14:55
  • I really appreciate you spending time for this, thank you so much. so youre saying that i should structure it by "car" not by "user"? – Nick Sep 26 '18 at 15:14
  • I got this structure from an online UBER app clone tutorial and i want it to do more. Like the driver can register 2 or more cars. – Nick Sep 26 '18 at 15:23
  • I say so because is not maintenable with lot of users and cars. However can you show driverRequests? Why do you declare snapshot if the callback already give one to you? – Andrew21111 Sep 26 '18 at 15:41
  • driverRequests is just "var driverRequests : [DataSnapshot] = []" im kinda used in using that "Database.database().reference().child..." in retrieving data from database – Nick Sep 26 '18 at 15:59
  • Remove let snapshot = self.driverRequests[indexPath.row] and try to print out snapshot.value, what you get ? – Andrew21111 Sep 26 '18 at 16:19
  • If i remove let snapshot = self.driverRequests[indexPath.row] I get the car lists of driver1 with the information in the debug area. In the simulator, My Cars page, its just Mazda Mazda – Nick Sep 27 '18 at 08:33
  • well, this is the behaviour you need, but when you do if let typeOfCar = driverRequestDictionary["car"] as? String, you need to do this for all the car on the dictionary – Andrew21111 Sep 27 '18 at 08:33
  • is there a way that i can retrieve the childByAutoID created by driver1 so at least can gather the information – Nick Sep 27 '18 at 09:21
  • 1
    Yes, for sure. check here at https://www.google.it/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/39328328/firebase-retrieve-childbyautoid-from-realtime-database%3Frq%3D1&ved=2ahUKEwi2tL3qpNvdAhWOM-wKHf88BykQjjgwAXoECAgQAQ&usg=AOvVaw177e31bLQquN-vjhwiHJvE – Andrew21111 Sep 27 '18 at 13:30
  • 1
    thanks @Andrea Abbate, I've manage to figure out some stuff with your help. – Nick Sep 27 '18 at 15:09
  • Hi @Andrea, is there a way that i can make the value of snapshot global, so that I can use them in UILabels? – Nick Sep 29 '18 at 12:46
  • Actually, I think it's a better way to access the UILabel into snapshot closure, but if you mean the UILabel of the cell, you need to store the snapshot value into an external variable. For example you put a class variable MyClass{ var actualSnapshot; viewDidLoad(){} ecc.. }, then when you get the snapshot you can do self.actualSnapshot = snapshot. If you want to do something after setting actual snapshot you can use this notation: var actualSnapshot: Type = Type(){ didSet{ self.collectionView.reloadData()} } for example – Andrew21111 Sep 29 '18 at 13:16
  • 1
    is it similar to this: https://stackoverflow.com/questions/45426984/how-to-pass-data-out-of-a-closure-that-retrieves-info-from-firebase – Nick Sep 29 '18 at 13:51