点击tableviewcell特定的Firebase数据到viewcontroller后如何接收?

时间:2017-07-27 11:40:54

标签: ios swift firebase tableview segue

我正在通过使用firebase构建一个swift应用程序,在我的firebase数据库中我有(childs)餐馆的信息。我已成功将firebase集成到我的项目中,并通过使用SDWebImage使用firebase数据填充我的tableview。但是现在我遇到了另一个与单击我的tableview单元格时使用segue和从firebase数据库中检索正确数据相关的问题。在过去,我已经使用detailViewControllers成功构建了几个swift和objective-c表视图,但这是我第一次使用firebase。 先感谢您 :) 在下面,我从xcode传递我的代码:

主要表格视图: 导入UIKit 导入Firebase 导入FirebaseAuth 导入FirebaseDatabase 导入FirebaseStorage 导入SDWebImage

类MainTableVC:UITableViewController {

@IBOutlet weak var MenuBar: UIBarButtonItem!



var barName = [String]()
var barMainImage = [String]()
var barAddress = [String]()





override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)



    MenuBar.target = self.revealViewController()
    MenuBar.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())


    getDataFromServer()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func getDataFromServer(){
    Database.database().reference().child("aktau").observe(DataEventType.childAdded, with: { (snapshot) in
        let values = snapshot.value! as! NSDictionary
        let posts = values["barmain"] as! NSDictionary
        let postIDs = posts.allKeys
        for id in postIDs{
            let singlePost = posts[id] as! NSDictionary
            self.barName.append(singlePost["barname"] as! String)
            self.barAddress.append(singlePost["baraddress"] as! String)
            self.barMainImage.append(singlePost["barmainimage"] as! String)

        }
        self.tableView.reloadData()

    })
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
     return barName.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RestoCell", for: indexPath) as! ViewControllerCell
    cell.RestName.text = barName[indexPath.row]
    cell.RestAddress.text = barAddress[indexPath.row]
    cell.RestImage.sd_setImage(with: URL(string: barMainImage[indexPath.row]))


    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "DetailView") {

        let VC = segue.destination as! DetailViewController
        VC.RestNameData = barName


}

} } barName问题 -

  

无法指定类型' [String]'的值输入' String?'

我的detailViewController 除了BarName,BarAddress和BarMainImage之外 - 我想包括短栏信息和开放时间。

导入UIKit 导入Firebase 导入FirebaseAuth 导入FirebaseDatabase 导入FirebaseStorage 导入SDWebImage

class DetailViewController:UIViewController {

@IBOutlet weak var MyScroll: UIScrollView!




@IBOutlet weak var RestDetailView: UIView!
@IBOutlet weak var restName: UILabel!

@IBOutlet weak var restTypeOfFood: UILabel!

@IBOutlet weak var restClockImage: UIImageView!

@IBOutlet weak var restOpenHours: UILabel!

@IBOutlet weak var restInfoView: UIView!

@IBOutlet weak var averagePrice: UILabel!

@IBOutlet weak var InfoAboutRest: UILabel!

@IBOutlet weak var FullAddressLabel: UILabel!

@IBOutlet weak var OpeningHoursLabel: UILabel!

var RestNameData: String?






override func viewDidLoad() {
    super.viewDidLoad()


    self.RestDetailView.layer.borderColor = UIColor.white.cgColor
    self.RestDetailView.layer.borderWidth = 1
    self.restInfoView.layer.borderColor = UIColor.white.cgColor
    self.restInfoView.layer.borderWidth = 1

    if RestNameData.text == "" {

    }

}

}

我的Firebase数据库树enter image description here

1 个答案:

答案 0 :(得分:1)

这是解决您面临的问题的一种方法。首先,最好使用单个变量来保存您在表格中显示的对象集合:即

// Define your model
struct Bar {
    var name: String
    var image: String
    var address: String
}

// Change your variables
var bars = [Bar]()
var selectedBar: Bar?

其次,您需要在选择单元格时获得单个bar对象。即。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    selectedBar = bars[indexPath.row]        
}

在进行segueing时,您可以将其注入detailsVC,如下所示:

details.selectedBar = selectedBar

您已完成selectedBar个对象以充气您的用户界面,而不仅仅是String

示例添加:我创建了一个执行所需功能的示例。在拉取代码时执行pod安装。这是我的数据库的样子:

enter image description here

存储库: https://github.com/HassanSE/FirebaseSample