PushViewController переход по нажатию кнопки из ячейки


#1

Здравствуйте.
Есть TableView, есть ячейка Cell с отдельным классом, в этой ячейке есть кнопка. Есть в проекте так же searchCotroller (UITableViewController), где я использую функцию для перехода по нажатию на ячейку и все работает прекрасно, переход осуществляется на нужного пользователя с его uid

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let user = filteredUsers[indexPath.row]
        print(user.username)
        
        searchBar.resignFirstResponder()
        
        let userProfileController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
        userProfileController.userId = user.uid
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        self.navigationController?.pushViewController(userProfileController, animated: true)
        
        tableView.deselectRow(at: indexPath, animated: true)
    }

Как по аналогии с вышеуказанным методом сделать переход на другой ViewController из ячейки, используя pushViewController именно при нажатии на кнопку в ячейке, а не на всю ячейку?
Спасибо.


#2
class MyCell: UITableViewCell {
    var actionHandler: ((MyCell) -> Void)?
    @IBAction func action() {
        actionHandler?(self)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
        cell.actionHandler = { [weak self] cell in
            //your logic here
        }
        return cell
    }

#3

Спасибо. Все работает!