简体   繁体   English

如何将数据从Firebase在tableview单元之间传递到视图控制器?

[英]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.

相关问题 如何在故事板中的两个视图控制器之间传递数据? - How to pass data between 2 view controllers in storyboards? 如何在 UITabBarController 中的视图控制器之间传递数据? - How to pass data between View Controllers in UITabBarController? 在Swift中从视图控制器之间传递数据(从TableView到DetailViewController) - Passing data between View Controllers in Swift (From TableView to DetailViewController) 如何将数据从tableview传递到另一个tableview单元? - how to pass data from tableview to another tableview cell? 如何将哪个tableview单元格作为导航标题传递给其他视图控制器? - How to pass what tableview cell was pressed to other view controllers as navigation title? 如何将单元格标签的值从tableView传递到其他View? - How to pass cell label's value from tableView to other View? 通过点击TableView单元格将数据传递到详细信息视图 - Pass Data to Detail View by Tapping TableView Cell 如何将 TableView 的回调传递给 TableView Cell? - How to pass callback from TableView to TableView Cell? 如何在这两个视图控制器之间快速传递数据 - how to pass data between these two view controllers in swift 你如何在 Swift 中的视图控制器之间传递数据? - How do you pass data between view controllers in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM