如何将Firebase数据从表视图单元传递到视图控制器?

时间:2019-01-28 17:00:36

标签: swift firebase

从现在开始,我已经成功地将数据从Firebase提取到单元中。但是,问题是我无法将单元格上的数据与下一个视图控制器上的标签相关联。更具体地说,我希望下一个ViewController上的标签在PostTableViewCell中显示我的priceTextLabel,这可能吗?

我是一个非常新的程序员,曾见过类似的案例,但未能将其应用于我的案例中。有人可以帮我个忙吗?

这是我的Post课

import Foundation


class Post {

var form:String
var subject:String
var region:String
var price:String
var createdAt:Date
var gender:String

init(form:String, subject:String, region:String, price:String, gender:String, timestamp:Double) {
    self.form = form
    self.subject = subject
    self.region = region
    self.price = price
    self.gender = gender
    self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
}
}

然后,我通过创建PostTableViewCell及其xib文件来制作单元格。

import UIKit

class PostTableViewCell: UITableViewCell {





override func awakeFromNib() {
super.awakeFromNib()
    // Initialization code
}

@IBOutlet weak var priceTextLabel: UILabel!
@IBOutlet weak var formTextLabel: UILabel!
@IBOutlet weak var subjectTextLabel: UILabel!
@IBOutlet weak var regionTextLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!



override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


func set(post:Post) {


    formTextLabel.text = post.form
    subjectTextLabel.text = "科目:"+post.subject
    regionTextLabel.text = "地區:"+post.region
    priceTextLabel.text = post.price
    subtitleLabel.text = post.createdAt.calenderTimeSinceNow()
}

}

这是我的视图控制器代码(包括表格视图单元格):

import Foundation
import UIKit
import Firebase
import FirebaseAuth

class HomeViewController:UIViewController, UITableViewDelegate, 
UITableViewDataSource {




var casetableView:UITableView!
var posts = [Post]()



override func viewDidLoad() {
    super.viewDidLoad()
    casetableView = UITableView(frame: view.bounds, style: .plain)

    let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
    casetableView.register(cellNib, forCellReuseIdentifier: "postCell")
    view.addSubview(casetableView)

    var layoutGuide:UILayoutGuide!


    if #available(iOS 11.0, *) {
        layoutGuide = view.safeAreaLayoutGuide
    } else {
        // Fallback on earlier versions
        layoutGuide = view.layoutMarginsGuide
    }
    casetableView.frame = CGRect(x: 0, y: 64, width: 414, height: 750)
    casetableView.rowHeight = UITableView.automaticDimension
    casetableView.delegate = self
    casetableView.dataSource = self
    casetableView.reloadData()


    observePosts()




}

func observePosts() {

    let postsRef = Database.database().reference().child("posts")
    let queryRef = postsRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 20)

    _ = queryRef.observe(.value, with: { snapshot in

        var tempPosts = [Post]()

        for child in snapshot.children {
            if let childSnapshot = child as? DataSnapshot,
                let dict = childSnapshot.value as? [String:Any],
                let form = dict["form"] as? String,
                let subject = dict["subject"] as? String,
                let region = dict["region"] as? String,
                let price = dict["price"] as? String,
                let gender = dict["gender"] as? String,
                let timestamp = dict["timestamp"] as? Double {


                let post = Post(form: form, subject: subject, region: region, price: price, gender: gender, timestamp: timestamp)
                tempPosts.insert(post, at: 0)
            }
        }

        self.posts = tempPosts
        self.casetableView.reloadData()

    })

}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
    cell.set(post: posts[indexPath.row])
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "CaseDetailsViewController") as? CaseDetailsViewController
self.navigationController?.pushViewController(vc!, animated: true)

}



}

最后,这是我在下一个View Controller上的代码,其中包括标签,但无法显示数据

import UIKit

class CaseDetailsViewController: UIViewController {


@IBOutlet var priceLbl: UILabel!


var pricess = ""



override func viewDidLoad() {
    super.viewDidLoad()
    priceLbl.text = pricess

}

}

1 个答案:

答案 0 :(得分:0)

didSelectRowAt上,您可以在变量pricess上设置发布属性值。

它应该像这样:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "CaseDetailsViewController") as? CaseDetailsViewController
    vc?.pricess = self.posts[indexPath.row].<#Attribute#>
    self.navigationController?.pushViewController(vc!, animated: true)

}