在我看来,你应该实现你的UITableViewCell的一个代表,当按钮接收触摸事件调用它的方法。例如:
protocol CellButtonDelegate: class {
func cellButton(cell: UITableViewCell, didTouch button: UIButton)
}
class Cell: UITableViewCell {
weak var delegate: CellButtonDelegate?
@IBAction func buttonTouched(sender: UIButton) {
delegate?.cellButton(cell: self, didTouch button: sender)
}
}
然后,让您的视图控制器成为每个表视图单元的委托并处理此事件。
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView. dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! Cell
// configure cell
cell.delegate = self
}
}
extension ViewController: CellButtonDelegate {
func cellButton(cell: UITableViewCell, didTouch button: UIButton) {
// handle button touch event, save indexPath of the cell to alter representation of table view after reloading data etc.
}
}
希望这会有所帮助。
显示你的代码*“发送动作来查看控制器,就像我通常做的一样”* ......如果没有看到你在做什么,就不可能给你任何帮助。 – DonMag