从 UITableView 推送数据以编程方式查看控制器
push data from UITableView to view controller programmatically
我正在尝试将数据从 tableView 推送到 View 控制器。我可以成功地把一些数据传输过来,但我仍然遗漏了一些关键点。我会尽力说明我的问题。在我的 notificationTableView 中,我存储了 userName、userImage、jobName 和 jobImage 等数据。我可以成功推送用户图像和名称,但是 jobName 和 JobImage 无法传输,如下图所示。
在此图像中,我们可以看到具有 userName、userImage、jobName 和 jobImage 的 tableView 部分。
在第二张图中,我们可以看到usersName和Image推送成功。但是,不会传输 jobImage 和名称。
我用来推送信息的代码是
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")
}
下面是我用来检索信息的代码。这是我的 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!)")
}
下面是我如何填充我的 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
}
如果我遗漏了任何有助于获得答案的信息,请告诉我。谢谢,麻烦您了。
作为讨论,您忘记为 jobProgressViewController
设置通知
在函数 didSelectRowAt indexPath
中添加以下代码:
acceptWorker.notification = notification
我正在尝试将数据从 tableView 推送到 View 控制器。我可以成功地把一些数据传输过来,但我仍然遗漏了一些关键点。我会尽力说明我的问题。在我的 notificationTableView 中,我存储了 userName、userImage、jobName 和 jobImage 等数据。我可以成功推送用户图像和名称,但是 jobName 和 JobImage 无法传输,如下图所示。
在此图像中,我们可以看到具有 userName、userImage、jobName 和 jobImage 的 tableView 部分。
在第二张图中,我们可以看到usersName和Image推送成功。但是,不会传输 jobImage 和名称。
我用来推送信息的代码是
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")
}
下面是我用来检索信息的代码。这是我的 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!)")
}
下面是我如何填充我的 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
}
如果我遗漏了任何有助于获得答案的信息,请告诉我。谢谢,麻烦您了。
作为讨论,您忘记为 jobProgressViewController
在函数 didSelectRowAt indexPath
中添加以下代码:
acceptWorker.notification = notification