Answer the question
In order to leave comments, you need to log in
How to pass data between two view controllers, indexPathForSelectedRow swift3?
Hello, friends. I have an error in passing data between two view controllers.
The "TheChoiseOfShipTableViewController" stores data in a class:
let ships: [Ships] = [
Ships(name: "Сухогрузное судно общего назначения", image: "Сухогрузное судно общего назначения.png"),
Ships(name: "Морское рефрижераторное судно", image: "Морское рефрижераторное судно.png")
]
var ships: Ships!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! DetailsTableViewCell
//отображение информации в ячейках
switch indexPath.row {
case 0:
cell.keyLabel.text = "Название"
cell.valueLabel.text =ships.name
default:
cell.keyLabel.text = ""
cell.valueLabel.text = ""
}
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
if let destination = segue.destination as? TheChoiseOfShipTableViewController {
let indexPath = CalculateViewController.indexPathForSelectedRow!
destination.ships = self.ships[indexPath.row]
}
}
}
Answer the question
In order to leave comments, you need to log in
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
if let destination = segue.destination as? TheChoiseOfShipTableViewController,
let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell) {
destination.ships = self.ships[indexPath.row]
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question