Swift中UITableView右侧侧滑按钮异常问题:滑动按钮区域时部分按钮消失且自动触发操作
解决UITableView侧滑按钮滑动时消失并自动触发操作的问题
嘿,我看你遇到了UITableView侧滑操作的一个头疼问题——配置了三个按钮,结果在按钮区域滑动时,两个按钮直接消失,还自动触发了操作。这其实是UIKit的默认“快速滑动执行”行为在搞鬼,咱们马上来fix它!
问题原因
UIKit默认开启了performsFirstActionWithFullSwipe这个属性,当你在侧滑出来的按钮区域继续向滑动方向(这里是右侧向左滑)滑动时,系统会认为你想快速执行第一个操作(也就是你数组里的rejectButton),于是自动收起其他按钮并触发该操作。
解决方案
只需要在创建UISwipeActionsConfiguration的时候,把这个默认行为关掉就行,设置performsFirstActionWithFullSwipe = false。这样用户在按钮区域滑动时,按钮不会消失,也不会自动触发任何操作,必须点击对应的按钮才会执行相应逻辑。
修改后的完整代码如下:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let viewButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in // View action print("View action") } viewButton.backgroundColor = ColorSet.appSkyLightColor viewButton.image = UIImage(named: "view") //Approve let approveButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in // approve action print("approve action") } approveButton.backgroundColor = ColorSet.appSkyLightColor approveButton.image = UIImage(named: "accept") //Reject let rejectButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in // Reject action print("Reject action") } rejectButton.backgroundColor = ColorSet.appSkyLightColor rejectButton.image = UIImage(named: "reject") let swipeActions = UISwipeActionsConfiguration(actions: [rejectButton, approveButton, viewButton]) // 关键:关闭快速滑动执行默认行为 swipeActions.performsFirstActionWithFullSwipe = false return swipeActions }
额外说明
如果你之后有需要开启特定场景的快速滑动执行,只需要把这个属性改回true就行。另外,确保你的按钮图片和颜色配置都正常,避免其他视觉上的问题哦~
内容的提问来源于stack exchange,提问作者A J
相关产品推荐
相关产品推荐

