Search code examples
iosswiftdatabasefirebaseviewcontroller

Passing Firebase Data from one view controller to detail view controller


I am looking to load data from my Firebase Database into a tableview in my main view controller and then pass that data to be more descriptive to a second view controller as my detail view controller. I am getting stuck because when i do click on the cell it will not segue to my other view controller and I am not sure how to get the data to pass. I want just the name on the first view controller and then the other data to populate on the detail view controller.

My Main View Controller--

import UIKit
import Foundation
import Firebase
import FirebaseDatabase

class ViewController: UIViewController {
    
    var table = [FacStaffInfo]()
    var ref: DatabaseReference!

    
    @IBOutlet weak var Tableview: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        ref = Database.database().reference().child("users")
        
        ref.observe(DataEventType.value, with: {(snapshot) in
            if snapshot.childrenCount > 0 {
                self.table.removeAll()
                
                for user in snapshot.children.allObjects as! [DataSnapshot] {
                    
                    let object = user.value as? [String: AnyObject]
                    let title = object?["title"]
                    let name = object?["name"]
                    let email = object?["email"]
                    let phone = object?["phone"]
                    let office = object?["office"]
                    let bio = object?["bio"]

                    let user = FacStaffInfo(title: title as! String, name: name as! String, email: email as! String, phone: phone as! Int, office: office as! String, bio: bio as! String)

                    self.table.append(user)
                    
                        self.Tableview.reloadData()
                    
                    
                    
                }
            }
        })
        
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return table.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! TableViewCell
        
        let user: FacStaffInfo
        
        user = table[indexPath.row]
        cell.titleLabel?.text = user.title
        cell.nameLabel?.text = user.name
        cell.emailLabel?.text = user.email
        cell.phoneLabel?.text = String(user.phone)
        cell.officeLabel?.text = user.office
        cell.bioLabel?.text = user.bio
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "showDetail", sender: self)
            }
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {

            if let indexPath = Tableview.indexPathForSelectedRow {
                let destinationController = segue.destination as! InfoViewController

                destinationController.FacStaffData = [table[indexPath.row]]
            }
        }
    }
}

My Detail View controller--

import UIKit
import Firebase
import FirebaseDatabase

class InfoViewController: UIViewController {
    
var FacStaffData = [FacStaffInfo]()
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var officeLabel: UILabel!
    @IBOutlet weak var bioLabel: UILabel!
    
    //var title = ""
    var name = ""
    var email = ""
    var phone = ""
    var office = ""
    var bio = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

        titleLabel.text = title
        nameLabel.text = name
        emailLabel.text = email
        phoneLabel.text = phone
        officeLabel.text = office
        bioLabel.text = bio
        
        print(titleLabel)
        
    }
}

My info class--

import Foundation
import Firebase
import FirebaseDatabase


class FacStaffInfo {
    
    var title: String
    var name: String
    var email: String
    var phone: Int
    var office: String
    var bio: String
    
    init(title: String, name: String, email: String, phone: Int, office: String, bio: String) {
        self.title = title;
        self.name = name;
        self.email = email;
        self.phone = phone;
        self.office = office;
        self.bio = bio
        
    }
}

and my tableview cell--

import UIKit
import Firebase
import FirebaseDatabase

class TableViewCell: UITableViewCell {
  
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var officeLabel: UILabel!
    @IBOutlet weak var bioLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

Solution

  • It looks like didSelectRowAt just implements didSelectRowAt again without calling anything.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                performSegue(withIdentifier: "showDetail", sender: self)
                }
        }
    

    try again with:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                    performSegue(withIdentifier: "showDetail", sender: self)
                    }
    

    Updated for followup:

    The InfoViewController is using the default values. When you prepare the segue, you put the information into FacStaffData. This should do the trick.

        class InfoViewController: UIViewController {
        
    var FacStaffData = [FacStaffInfo]()
        
        @IBOutlet weak var titleLabel: UILabel!
        @IBOutlet weak var nameLabel: UILabel!
        @IBOutlet weak var emailLabel: UILabel!
        @IBOutlet weak var phoneLabel: UILabel!
        @IBOutlet weak var officeLabel: UILabel!
        @IBOutlet weak var bioLabel: UILabel!
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            titleLabel.text = FacStaffData.title
            nameLabel.text = FacStaffData.name
            emailLabel.text = FacStaffData.email
            phoneLabel.text = FacStaffData.phone
            officeLabel.text = FacStaffData.office
            bioLabel.text = FacStaffData.bio
            
            print(titleLabel)
            
        }
    }