首先,您应该编写一个协议,例如:
protocol CustomCellDelegate {
func doAnyAction(cell:CustomUITableViewCell)
}
Run Code Online (Sandbox Code Playgroud)
然后在您的自定义单元格类中声明:
weak var delegate:CustomCellDelegate?
Run Code Online (Sandbox Code Playgroud)
并在自定义单元类的IBAction中:
@IBAction func onButtonTapped(_ sender: UIButton) {
delegate?.doAnyAction(cell: self)
//here we say that the responsible class for this action is the one that implements this delegate and we pass the custom cell to it.
}
Run Code Online (Sandbox Code Playgroud)
现在在您的viewController中:
1-使您的视图控制器实现CustomCellDelegate。2-在您的cellForRow声明单元格时,不要忘了写:
cell.delegate = self
Run Code Online (Sandbox Code Playgroud)
3-最后在您的ViewController中调用该函数:
func doAnyAction(cell: CustomUITableViewCell) {
let row = cell.indexPath(for: cell)?.row
//do whatever you want
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1092 次 |
| 最近记录: |