Ad

Passing Data From Firebase Database From UITableviewCell To Next ViewController Not Showing

I've already searched through some of the answers that have been asked about this but none of them seem to apply/work in my situation which is why I've decided to ask the community. I am simply trying to pass the data from the table view cell to the next view controller. I have gotten as far as being able to display the information in my cells accurately however whenever i select the row it just shows the view controller with no information

I have tried to set the labels and pictures to whatever the UITableViewCell may show but it is not working. I created an NSObject class that defines the variables which is why it is confusing me as to how to pass the data through to the next view Controller.

This is my AddFriendViewController where I fetch the users from Firebase and it displays my information on the tableview

class AddFriendViewController: UIViewController {

var users = [Users]()


var databaseRef = Database.database().reference()


@IBOutlet weak var friendsTableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    friendsTableView.delegate = self
    friendsTableView.dataSource = self

    fetchUser()

}

func fetchUser() {
    databaseRef.child("users").observe(.childAdded) { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = Users()
            user.nameOfUser = dictionary["nameOfUser"] as? String ?? ""
            user.email = dictionary["email"] as? String ?? ""
            user.profileImageURL = dictionary["profileImageURL"] as? String ?? ""

            self.users.append(user)

            DispatchQueue.main.async {
                self.friendsTableView.reloadData()
            }
        }
    }

 }


}

extension AddFriendViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.users.count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let friendCell = UITableViewCell(style: .subtitle, reuseIdentifier: "friendCell")


    let user = users[indexPath.row]
    friendCell.textLabel?.text = user.nameOfUser
    friendCell.detailTextLabel?.text = user.email


    if let profileImageURL = user.profileImageURL {
        let url = URL(string: profileImageURL)
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil {
                print(error)
                return
            }
            DispatchQueue.main.async {

                friendCell.imageView?.image = UIImage(data: data!)

              }

            }.resume()
    }

    return friendCell

}

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

    performSegue(withIdentifier: "showFriendProfile", sender: self.users[indexPath.row])
    self.friendsTableView.deselectRow(at: indexPath as IndexPath, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showFriendProfile" {
        if let indexPath = friendsTableView.indexPathForSelectedRow {
            let dvc = segue.destination as! DetailViewController

            ***This is where I am confused as to what I should be doing***
            //EDIT1: 
            print("The nameOfUser is \(user.nameOfUser!)")
            print("The email is \(user.email!)")

        }
    }
  }

}

This is my Users Class:

class Users: NSDictionary {
    var nameOfUser: String?
    var email: String?
    var profileImageURL: String?

}

This is my DetailViewController:

class DetailViewController: UIViewController {

var nameOfUser = String()
var email = String()
var profileImageURL = UIImage()

var ref: DatabaseReference?


@IBOutlet weak var profileImageView: UIImageView!

@IBOutlet weak var nameOfUserLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    nameOfUser = nameOfUserLabel.text!
    email = emailLabel.text!
    profileImageURL = profileImageView.image!

    }

}

The obvious goal is to simply click on the cell to show the data on the next view controller. I understand similar questions have been asked in the past but I truly don't know how to use those as the solution to my problem. Any help will be greatly appreciated and please let me know if there is anything I need to clarify.

EDIT1: I added the print statement on the prepare for segue function and noticed it is at least pulling the information but for some reason not passing it to the next view controller.

Thank you

Ad

Answer

You just need to get your sender and set the properties of the detail view controller.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showFriendProfile" {
        guard let dvc = segue.destination as? DetailViewController else {
            return
        }

        if let user = sender as? Users {
            DispatchQueue.main.async {
                dvc.nameOfUserLabel.text = user.nameOfUser
                dvc.emailLabel.text = user.email
                let url = URL(string: user.profileImageURL!)
                let data = try? Data(contentsOf: url!)
                dvc.profileImageView.image = UIImage(data: data!)
            }
        }
    }
}
Ad
source: stackoverflow.com
Ad