0
votes

I am building a swift application through the use of firebase, within my firebase database I have (childs) restaurants with the information. I have successfully integrated firebase to my project and populating my tableview with firebase data with images through the use of SDWebImage. However now I am stuck with another issues relating the use of segue and retrieving correct data from my firebase database upon clicking my tableview cell. In the past I have successfully built several swift and objective-c tableviews with the detailViewControllers, but it is my first experience with firebase. Thank you in advance :) Here below I my passing my codes from xcode:

Main Tableview: import UIKit import Firebase import FirebaseAuth import FirebaseDatabase import FirebaseStorage import SDWebImage

class 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


}

} } issue with barName -

Cannot assign value of type '[String]' to type 'String?'

And my detailViewController In Addition to the BarName, BarAddress and BarMainImage - I would like to include short bar information and opening time.

import UIKit import Firebase import FirebaseAuth import FirebaseDatabase import FirebaseStorage import 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 == "" {

    }

}

}

My Firebase Database Tree: enter image description here

1

1 Answers

1
votes

Here's one way to fix problem you're facing. First, it's better to use a single variable to hold your collection of objects you're displaying in table: i.e.

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

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

Second, you'd need to get a single bar object upon cell's selection. i.e.

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

Upon segueing, you can inject it to your detailsVC like so:

details.selectedBar = selectedBar

You've complete selectedBar object to inflate your UI instead of just a String.

Sample Addition: I've created a sample that does the required functionality. Perform the pod installation as you pull the code. Here's what my database looks like:

enter image description here

Repository: https://github.com/HassanSE/FirebaseSample