если вы не хотите выполнять свой код в методе didSelectRowAt, другой хороший подход, на мой взгляд, заключается в создании делегата вашей пользовательской ячейки. Смотрите код ниже
// This is my custom cell class
class MyCustomCell: UITableViewCell {
// The button inside your cell
@IBOutlet weak var actionButton: UIButton!
var delegate: MyCustomCellDelegate?
@IBAction func myDelegateAction(_ sender: UIButton) {
delegate?.myCustomAction(sender: sender, index: sender.tag)
}
// Here you can set the tag value of the button to know
// which button was tapped
func configure(index: IndexPath){
actionButton.tag = index.row
}
}
protocol MyCustomCellDelegate {
func myDelegateAction(sender: UIButton, index: Int)
}
Делегируйте ViewController, где вы используете свою пользовательскую ячейку.
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellIdentifier", for: indexPath) as! MyCustomCell
cell.configure(index: indexPath)
cell.delegate = self
return cell
}
}
И в конце настройте свой метод, расширяя свой пользовательский делегат ячейки
extension MyViewController: MyCustomCellDelegate {
func myDelegateAction(sender: UIButton, index: Int) {
// Do your staff here
}
}
Надеюсь, я помог.