1

I'm trying to recognize which tableviewcell is selected and after that i want to get the label value of that cell and pass it to the next view controller. My cell has a Label value which is string and a Number value which is Int. I'm using a firebase database to get all that data.

My code:

import UIKit


class PlacesTableViewController: UITableViewController {
 //MARK: Properties
    @IBOutlet weak var placesTableView: UITableView!
var places = [Places]()
override func viewDidLoad()
    {
        super.viewDidLoad()




        // Loads data to cell.
        loadData()
    }
override func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {   
        //return the number of rows
        return places.count
    }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "PlacesTableViewCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell  else {
            fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
        }

        let place = places[indexPath.row]

        cell.placeLabel.text = place.name
        cell.ratingControl.rating = place.rating

        return cell

    }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        print(places[indexPath.section])
        self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: nil)
    }
}
Alex Andreadis
  • 277
  • 4
  • 13

2 Answers2

1

you can follow this thread to be useful send-data-from-tableview-to-detailview-swift

You have to create variable in destination view controller and store data in those Variable before Moving to Destination View controller. This link will help you for this.

Community
  • 1
  • 1
elk_cloner
  • 1,843
  • 1
  • 9
  • 11
0

Try this:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        let selectedPlace = places[indexPath.section]
        self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: selectedPlace)
    }
}

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

    if let selectedPlace = sender as? Places,
        let destViewController = segue.destination as? SecondViewController {
        destViewController.place = selectedPlace
    }
}
pesch
  • 1,866
  • 2
  • 14
  • 26