Добавьте делегата в вашу ячейку и назначьте его представителю VC.См. Ниже.
Использование делегирования
Создание пользовательской ячейки, наследуемой от UITableViewCell.
class CustomCell: UITableViewCell {
var cellDelegate : CustomCellDelegate = nil
@IBAction func elementTapped() {
cellDelegate?.launchVC()
}
}
Пользовательский делегат ячейки
protocol CustomCellDelegate {
func launchVC()
}
MainViewController
class ViewController: UIViewController, UITableViewControllerDataSource, UITableViewDelegate {
IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell {
// important
cell.delegate = self
return cell
}
}
}
Расширение ViewController для реализации протокола
extension ViewContrller: CustomCellDelegate {
func launchVC() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
self.present(vc, animated: true, completion: nil)
}
}