要实现在表视图中点击按钮时不执行任何操作,可以通过以下方法来解决:
func buttonClicked(_ sender: UIButton) {
if !tableView.isUserInteractionEnabled {
return
}
// 执行按钮点击后的操作
// ...
}
tableView(_:didSelectRowAt:)
代理方法中取消选中状态,以防止通过点击行来触发按钮的点击事件。func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
tableView(_:cellForRowAt:)
代理方法中为按钮添加一个空的点击事件,以防止按钮被点击时触发其他操作。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
// 配置其他 cell 的内容
cell.button.addTarget(self, action: #selector(emptyButtonClicked), for: .touchUpInside)
return cell
}
@objc func emptyButtonClicked() {
// 空的按钮点击事件,不执行任何操作
}
通过以上方法,可以实现在表视图中点击按钮时不执行任何操作。
下一篇:不要调整隐藏的行