A
A
Aleksandr Govorukhin2017-03-04 10:00:21
iOS
Aleksandr Govorukhin, 2017-03-04 10:00:21

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")
]

They need to be passed to the "CalculateViewController", there is such an implementation:
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]
            }
        }
    }

The error is that it swears at ships..name, because it is equal to nil. I need to pass data, but I have an error in indexPathForSelectedRow. Tell me how to competently implement the last piece of code for data transfer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iMike, 2017-03-05
@SnapSh0t

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]
            }
        }
    }

B
briahas, 2017-03-10
@briahas

swears at ships..name because it is equal to nil.
Probably because you didn't complete it. How do you pass ships..name to CalculateViewController ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question