Search code examples
iosxcodeuitableviewpushpushviewcontroller

push data from UITableView to view controller programmatically


I am trying to push data from a tableView to a View controller. I can successfully transfer some of the data over, but I am still missing some key points. I will try to illustrate my question to the best of my abilities. In my notificationTableView, I have data that is stored such as a userName, userImage, jobName and jobImage. I can succesfully push over the users image and name, however The jobName and JobImage fails to be transferred over as we can see in the Images below.

firstImage

In this image, we can see the tableView sections that have the userName, userImage, jobName and jobImage.

second image

In the second image, we can see that the usersName, and Image is succesfully pushed. However, the jobImage and name are not transferred.

the code that I use to push over the information is

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

    let notification = notifications[indexPath.row]
    if notification.notificationType != .swipe {

        let acceptWorker = jobProgressViewController()
        acceptWorker.workerUser = myUser
        acceptWorker.workerUser = notification.user
        jobProgressView?.myParentViewController = self
        let navController = UINavigationController(rootViewController: acceptWorker)
                  present(navController, animated: true, completion: nil)

    } else {
   print("something else should go here")
}

and the code that I use to retrieve the information is below. which is my jobProgressViewController

     var notification: userNotifications?
     var workerUser: User? {

didSet {
    let name = workerUser?.name
    workerNameLabel.text = name


    guard let profileImage = workerUser?.profileImageUrl else { return }
    workerImageView.loadImageUsingCacheWithUrlString(profileImage)

    if let post = notification?.poster {
        jobImageView.loadImageUsingCacheWithUrlString(post.imageUrl1!)
        jobLabel.text = post.category
        addressLabel.text = post.category
                      }

}
}

   fileprivate func setupView(){
    let postUser = workerUser.self
    let uid = Auth.auth().currentUser?.uid
    let userName = postUser?.name
    let posterId = postUser?.uid
    let post = notification?.poster

    guard let userImage = workerUser?.profileImageUrl else { return }
    Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String : Any] else { return }
        let user = User(dictionary: dictionary as [String : AnyObject])

        let currentUser = MyUser(dictionary: dictionary as [String : AnyObject])
        self.posterImageView.image = #imageLiteral(resourceName: "user")
        self.posterImageView.loadImageUsingCacheWithUrlString(userImage)

        self.userNameLabel.text = userName
        self.userNameLabel.font = UIFont.systemFont(ofSize: 30)
        self.userNameLabel.textColor = UIColor.black

        self.workerImageView.image = #imageLiteral(resourceName: "user")
        self.workerImageView.loadImageUsingCacheWithUrlString(currentUser.profileImageUrl!)

        self.workerNameLabel.text = currentUser.name
        self.workerNameLabel.font = UIFont.systemFont(ofSize: 30)
        self.workerNameLabel.textColor = UIColor.black


        self.addressLabel.text = postUser?.address
        self.addressLabel.font = UIFont.systemFont(ofSize: 30)
        self.addressLabel.textColor = UIColor.black

        self.jobLabel.text = post?.category
        self.jobLabel.font = UIFont.systemFont(ofSize: 30)
        self.jobLabel.textColor = UIColor.black



    }, withCancel: { (err) in
        print("attempting to load information")

    })
     print("this is your uid \(posterId!)")
}

below is how I populate my notificationCell which shows the users information in my tableView

    var jobProgressView: jobProgressViewController? = nil

 var delegate: NotificationCellDelegate?

var notification: userNotifications? {
    didSet {


       guard let user = notification?.user else { return }
       guard let profileImageUrl = user.profileImageUrl else { return }


        profileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)
        configureNotificationLabel()
        configureNotificationType()

        if let post = notification?.poster {
            postImageView.loadImageUsingCacheWithUrlString(post.imageUrl1!)
        }
    }
}

    func configureNotificationLabel() {
    guard let notification = self.notification else { return }
    guard let user = notification.user else { return }
    guard let poster = notification.poster else { return }
    guard let username = user.name else { return }
    guard let notificationDate =  configureNotificationTimeStamp() else { return }
    guard let jobName = poster.category else { return }

    let notificationMessage = notification.notificationType.description

    let attributedText = NSMutableAttributedString(string: username, attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)])

    attributedText.append(NSAttributedString(string: notificationMessage , attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.black]))

    attributedText.append(NSAttributedString(string: jobName, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.black]))

    attributedText.append(NSAttributedString(string: " \(notificationDate).", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.gray]))
        notificationLabel.attributedText = attributedText
}

if there is anyInformation I may have left out to help with getting an answer please let me know. please and thank you.


Solution

  • As discusstion, you forget set notification for jobProgressViewController

    In func didSelectRowAt indexPath add below code:

    acceptWorker.notification = notification