[英]how to pass data from firebase between tableview cell to view controllers?
Hi guys I'm really having troubles trying to pass data from a tableview to another view controller. 嗨,大家好,我真的很难将数据从一个tableview传递到另一个view controller。 I have a tableview that displays a list of every user but when I click on a user it goes to the next view controller without showing any data of that particular user.
我有一个显示每个用户列表的表视图,但是当我单击一个用户时,它会转到下一个视图控制器,而不会显示该特定用户的任何数据。 The code below is pretty much the same from an example I followed but for some reason its not working for me.
下面的代码与我遵循的示例几乎相同,但由于某种原因,它对我不起作用。 Please help, I've tried searching for solutions but can't find anything.
请帮忙,我尝试过寻找解决方案,但找不到任何东西。 Im also using firebase to store and retrieve my data.
我还使用Firebase来存储和检索我的数据。 Thanks in advance
提前致谢
@IBOutlet weak var searchTableView: UITableView!
var profiles = [user]()
var ref: FIRDatabaseReference!
override func viewdidload(){
databaseRef = FIRDatabase.database().reference()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "guestSegue", sender: self)
self.searchTableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "guestSegue" {
if let indexpath = searchTableView.indexPathForSelectedRow {
let guestVC = segue.destination as! guestProfileViewController
guestVC.ref = profiles[indexpath.row].ref
}
}
}
user array 用户数组
class User {
var backgroundProfileImage: String!
var fullName: String!
var profilePic: String!
var ref: FIRDatabaseReference!
var key: String
init(backgroundProfileImage: String, fullName: String, profilePic: String, key: String = ""){
self.backgroundProfileImage = backgroundProfileImage
self.fullName = fullName
self.profilePic = profilePic
self.key = key
self.ref = FIRDatabase.database().reference()
}
init(snapshot: FIRDataSnapshot){
self.backgroundProfileImage = value["backgroundProfileImage"] as! String!
self.fullName = (snapshot.value as! NSDictionary)["fullName"] as! String
self.profilePic = (snapshot.value as! NSDictionary)["profilePic"] as! String
self.uid = (snapshot.value as! NSDictionary)["uid"] as! String!
self.key = snapshot.key
self.ref = snapshot.ref
}
guestViewController guestViewController
class guestProfileViewController: UIViewController, UINavigationControllerDelegate {
@IBOutlet weak var profileImage: UIImage!
@IBOutlet weak var backgroundProfileImage: UIImageView!
@IBOutlet weak var fullNameLabel: UILabel!
var ref: FIRDatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
loadProfileData()
}
func loadProfileData(){
if let ref = ref {
ref.observe(.value, with: { (user) in
let user: User = User(snapshot: user)
self.fullNameLabel.text = user.fullName
self.profileImage.sd_setImage(with: URL(string: user.profilePic), placeholderImage: UIImage(named: "default"))
self.backgroundProfileImage.sd_setImage(with: URL(string: user.backgroundProfileImage), placeholderImage: UIImage(named: "default_1"))
})
}
}
}
ref is being set in viewDidLoad() of guestProfileViewController. ref是在guestProfileViewController的viewDidLoad()中设置的。 Try removing that bit as it seems like you want ref to equal the User's ref.
尝试删除该位,因为您希望参考与用户的参考相等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.