нажатие кнопки внутри UITableViewCell не работает - PullRequest
0 голосов
/ 26 июня 2019

Я пытался связать кнопку, даже используя делегаты для моего UITableViewCell, но событие никогда не вызывалось.

Я выполняю следующие действия: https://stackoverflow.com/a/40806707/7746248

Я все перепроверил, может быть, я что-то упустил, и, кажется, нет камней, оставленных без поворота.

У меня нет объяснения, почему это не должно работать!

Это мой код ниже:

protocol OptionButtonsDelegate: class {
    func closeFriendsTapped(at index:IndexPath)
}
class MyCell: UITableViewCell {
    weak var delegate:OptionButtonsDelegate?
    var indexPath:IndexPath!
    @IBAction func closeFriendsAction(_ sender: UIButton) {
        self.delegate?.closeFriendsTapped(at: indexPath)
    }
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, OptionButtonsDelegate {

var tableArray = [MyData] ()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()

// set data & reload
self.tableView.reloadData()
}

func closeFriendsTapped(at index: IndexPath) {
    print("button tapped at index:\(index)")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
            as! MyCell
        cell.delegate = self
        cell.indexPath = indexPath

        return cell
    }
}
...