ios - 如何在 tableview 单元格之间将数据从 firebase 传递到 View Controller ?

标签 ios swift uitableview firebase

大家好,我在尝试将数据从 tableview 传递到另一个 view controller 时遇到了麻烦。我有一个显示每个用户列表的 TableView ,但是当我单击一个用户时,它会转到下一个 View Controller 而不显示该特定用户的任何数据。下面的代码与我遵循的示例几乎相同,但出于某种原因,它对我不起作用。请帮忙,我试过寻找解决方案但找不到任何东西。我还使用 firebase 来存储和检索我的数据。提前致谢

@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


        }
    }
}

用户数组

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

}

guest View Controller

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 正在 guestProfileViewController 的 viewDidLoad() 中设置。尝试删除该位,因为您似乎希望 ref 等于用户的 ref。

关于ios - 如何在 tableview 单元格之间将数据从 firebase 传递到 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43723308/

相关文章:

ios - 滚动时添加到 UITableViewCell 的图像出现在错误的行中

iOS - 将 UISearchDisplayController 与 UISearchBar 一起使用,即 UIToolbar 中的 UIBarButtonItem

swift - 在 Swift 中组合相似的结构

ios - 异步任务完成时的返回值

ios - .ondelete 带有部分的 SwiftUI 列表

ios - 设备旋转中断后 iOS 11 中的 UITableViewCell 操作

ios - 将 UILongPressGestureRecognizer 添加到自定义 UITableViewCell 中的 UIButton

ios - 如何让 Playground 执行时间和我们在 iOS 应用程序中运行一样快

ios - 如何在 willDisplayCell 方法中配置 UITableViewCell?

iphone - 如何从 3.875 小时中分离和存储小时和分钟