-2

I want to pass the whole User info, I have a User class. I want to pass the username,name ,uid, image to another view controller.

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

        let user = usersarray[indexPath.row]
        let ColorView = UIView()
        ColorView.backgroundColor = UIColor(red: 0.05, green: 0.15, blue: 0.28, alpha: 0.7)
        cell.selectedBackgroundView = ColorView
        cell.backgroundColor = UIColor.clear
        cell.backgroundView?.backgroundColor = UIColor.clear
        cell.firstname.text = user.firstname! + " " + user.lastname!
        cell.username.text = "@" + user.username!
        cell.userimage?.layer.masksToBounds = true
        cell.userimage.layer.cornerRadius = 20
        cell.userimage.clipsToBounds = true
        if let profileimage = user.profilepic
        {cell.userimage.loadImageCache(urlString: profileimage)}
        return cell
    }

    func callmessage(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "message")
        self.present(controller, animated: true, completion: nil)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let indexPath = tableView.indexPathForSelectedRow!
        callmessage(user: User!)
    }
  • Hint: `prepareForSegue()`. Set up variables/array in your destination viewcontroller to receive this data and assign the values within this method in your current viewcontroller. Good luck! – Louis Leung Nov 18 '17 at 03:54
  • I would recommend - at first at least - using a `struct` for handling data in TableViews. So that referencing the data in another ViewController is as easy as creating a global Object of that type. – KSigWyatt Nov 18 '17 at 04:50
  • you can also create a tuple in destination class and pass the tuple from one class to another class with all value – Girijesh Kumar Nov 18 '17 at 05:03
  • I do have a User Class, different file, Should I call that File in the other view controller, or inside of the didselectedrowatindexpath? – Gabriel Tavares Nov 18 '17 at 21:32

1 Answers1

0

Subclass the UITableViewCell (create a new file of type UITableViewCell and call it something like UserTableViewCell. Add a property of type User (or whatever your user class is). On DidSelectRowAtIndexPath set the user property of the UIViewController equal to the User property of the UserTableViewCell, and then present the view controller.

Scotch Design
  • 1,016
  • 9
  • 10