【问题标题】:push data from UITableView to view controller programmatically以编程方式将数据从 UITableView 推送到视图控制器
【发布时间】:2020-05-25 09:36:19
【问题描述】:

我正在尝试将数据从 tableView 推送到 View 控制器。我可以成功地将一些数据传输过来,但我仍然缺少一些关键点。我会尽力说明我的问题。在我的notificationTableView 中,我存储了诸如userName、userImage、jobName 和jobImage 之类的数据。我可以成功推送用户图像和名称,但是正如我们在下面的图像中看到的那样,jobName 和 JobImage 无法传输。

在这张图片中,我们可以看到包含 userName、userImage、jobName 和 jobImage 的 tableView 部分。

在第二张图片中,我们可以看到usersName和Image被成功推送。但是,jobImage 和 name 不会被传输。

我用来推送信息的代码是

    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")
}

我用来检索信息的代码如下。这是我的工作ProgressViewController

     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!)")
}

下面是我如何填充我的 notificationCell,它在我的 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
}

如果有任何我可能遗漏的信息来帮助获得答案,请告诉我。谢谢,麻烦您了。

【问题讨论】:

  • 我认为它确实以某种方式回答了这个问题,但是,我正在以编程方式编码,这些答案使用 xnib 或故事板。我会尝试更多地挖掘它,看看是否能找到对我的特定答案有帮助的答案。
  • setupView 何时会被调用?我认为在函数setupView 中应该将workerUser 的数据绑定到UI 并在viewDidLoad() 中调用setupView
  • setupView 在 ViewDidLoad() 中被调用
  • 您在哪里将通知数据设置为jobProgressViewController
  • 在我的 notificationView 控制器中。这就是你要找的东西吗?

标签: ios xcode uitableview push pushviewcontroller


【解决方案1】:

在讨论中,您忘记为jobProgressViewController 设置通知

在 func didSelectRowAt indexPath 中添加以下代码:

acceptWorker.notification = notification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多