Firebase数据在表视图和导航视图之间传递

时间:2017-09-15 07:20:42

标签: swift uitableview firebase firebase-realtime-database

我正在尝试从单击导航控制器的单元格中获取数据。 (然后标签中的数据。我想要一种额外的信息标签)这是我到目前为止所得到的:

这是mij Post课程:

class Post {
    let ref: DatabaseReference!
    var TypeControle: String = ""
    var Stad: String = ""
    var Tijd: String = ""
    var Latitude: String = ""
    var Longitude: String = ""
    var Extrainformatie: String = ""

    init(TypeControle: String) {
        self.TypeControle = TypeControle
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }
    init(Stad: String){
        self.Stad = Stad
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }
    init(Tijd: String) {
        self.Tijd = Tijd
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }
    init(Latitude: String) {
        self.Latitude = Latitude
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }
    init(Longitude: String) {
        self.Longitude = Longitude
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }
    init(Extrainformatie: String) {
        self.Extrainformatie = Extrainformatie
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }

    init() {
        ref = Database.database().reference().child("Rollerbanken").childByAutoId()
    }

    init(snapshot: DataSnapshot)
    {
        ref = snapshot.ref
        if let value = snapshot.value as? [String : Any] {
            TypeControle = value["TypeControle"] as! String
            Stad = value["Stad"] as! String
            Tijd = value["Tijd"] as! String
            Latitude = value["Latitude"] as! String
            Longitude = value["Longitude"] as! String
            Extrainformatie = value["Extrainformatie"] as! String

        }
    }

    func save() {
        ref.setValue(toDictionary())
    }

    func toDictionary() -> [String : Any]
    {
        return [
            "TypeControle" : TypeControle,
            "Stad" : Stad,
            "Tijd" : Tijd,
            "Latitude" : Latitude,
            "Longitude" : Longitude,
            "Extrainformatie" : Extrainformatie
        ]
    }
}

数据库结构: Structure

TableviewCell: enter image description here

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "mySegue" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let controller = segue.destination as! ExtraViewController
                let story = posts[indexPath.row]
                controller.tijd = story["Tijd"] as! String
            }
    }
}

我得到的错误是:输入'Post'没有下标成员。

在线:controller.tijd = story["Tijd"] as! String

2 个答案:

答案 0 :(得分:0)

试试这个: -

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "mySegue" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let controller = segue.destination as! ExtraViewController
            let story = posts[indexPath.row]
             print(story.Tijd)
            controller.tijd = story.Tijd as! String
        }
  }
}
ExtraViewController中的

: -

var tijd:String!

我认为你的数组包含post model类型对象。

答案 1 :(得分:0)

你得到的错误给我们一个问题的暗示:

Type 'Post' has no subscript members

此处story(在story = posts[indexPath.row]中)的类型为Post,Post没有在其类范围内定义的下标。这就是为什么当你尝试编写story["Tijd"]时会抛出错误。

从我所看到的情况来看,我相信你收到它时应该对你的快照进行可选的类型转换as? [String : AnyObject],但我不确定你是如何初始化快照的。{/ p>