在我看來,你應該實現你的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