0

Right now I have a table view and another view controller. I would like to use the didSelectRowAt function to show a detailed view of the tableview cell on the new view controller. How would I pass data from the the tableview cell to a new view controller? Here is what I currently have.

This is the code for my main view controller.

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    struct data {
        var sectionTitle = String()
        var rowTitles = [String]()
    }

    var dataArray = [data(sectionTitle: "section 1", rowTitles: ["row 1", "row 2", "row 3"]),
        data(sectionTitle: "section 2", rowTitles: ["row 1", "row 2"]),
        data(sectionTitle: "section 3", rowTitles: ["row 1"])
       ]
    var searchArray = [(sectionTitle: String, rowTitles: [String])]()
    var searching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchArray[section].rowTitles.count
        } else {
            return dataArray[section].rowTitles.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
        if searching {
            cell?.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
        } else {
            cell?.textLabel?.text = self.dataArray[indexPath.section].rowTitles[indexPath.row]
        }
        return cell!
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if searching {
        return searchArray.count
        } else {
        return dataArray[section].sectionTitle
        }
    }
}

This is the code for my second view controller so far.

import UIKit
    class secondViewController: UIViewController {

    @IBOulet weak var nameLabel: UILabel!
    @IBOulet weak var descriptionLabel: UILabel!
    @IBOulet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func visitButtonTapped(_ sender: Any) {

    }

}

Here is my current Storyboard. enter image description here

1 Answers1

0

Create a model in your ViewController import UIKit class secondViewController: UIViewController {

@IBOulet weak var nameLabel: UILabel!
@IBOulet weak var descriptionLabel: UILabel!
@IBOulet weak var imageView: UIImageView!
var titleOfItem : String
override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func visitButtonTapped(_ sender: Any) {

}

}

In your tableView delegate

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // Init you secondVC
   secondVC.titleOfItem = searchArray[indexPath.section].rowTitles[indexPath.row]
   // pushToVC / Present

}
King.lbt
  • 843
  • 5
  • 15