Search code examples
iosswiftuitableviewuiviewcontrolleruikit

Passing a Struct Array through a TableViewCell to another ViewController


I'm new to swift and I've been stuck on this for a while now I'm trying to pass a Struct Array from a tableview cell to another view


Solution

  • If you want to share specific cell's data into table of other view controller, then you need to create an object of Model struct rather than its array just like below:

    var newFeed: Model?
    

    Next you can assign it value of particular cell before navigation in main did select method as below:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let vc = storyboard?.instantiateViewController(withIdentifier: "Comment") as? Comment else {return}
        vc.newFeed = getInfo[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
    }
    

    It will assign that cell's data to newFeed Variable and relaod table with that data.

    For any question, feel free to ask.