1

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
    }
}
MattyJay94
  • 39
  • 1
  • 8

1 Answers1

0

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)
        
    }
}
DPrice
  • 164
  • 2
  • 6
  • Thanks! I did that and now when i go to the next page it doesnt show any data? – MattyJay94 Sep 08 '20 at 23:40
  • I updated the answer with a correction to the InfoViewController – DPrice Sep 09 '20 at 01:40
  • When I add those to the class it give me an error saying "Value of type '[FacStaffInfo]' has no member 'title'" but for each one. But i see them in the FacStaffInfo class. could i be missing something in there? – MattyJay94 Sep 09 '20 at 03:12
  • Sorry I didn't see that FacStaffInfo is defined as an array of FacStaffData Change it from `var FacStaffData = [FacStaffInfo]()` to `var FacStaffData = FacStaffInfo!` and go to prepare segue and change `destinationController.FacStaffData = [table[indexPath.row]]` to `destinationController.FacStaffData = table[indexPath.row]` I think that will do it. – DPrice Sep 09 '20 at 04:24
  • So when i went back I changed the var FacStaffData = [FacStaffInfo]() to var FacStaffData : FacStaffInfo? and add a ? behind it in the FacStaffData.Var and it worked there. So now the only issue is loading my Int for the phone number in as a integer and not a string. – MattyJay94 Sep 09 '20 at 13:53
  • We got the initial problem solved. How to store and represent the phone number is a different question. There is a good discussion on that topic here https://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers – DPrice Sep 09 '20 at 19:00
  • It sounds like the best solution is to use strings. – DPrice Sep 09 '20 at 19:01