如何禁用iOS11中tableview单元格的完全滑动

时间:2017-09-25 21:49:43

标签: ios uitableview swipe ios11 swift4

UITableViewDelegate.h

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

但是,我在我的trailingActions方法中返回nil,我仍然可以在我的tableview中完全滑动删除。如何防止完全滑动? (我希望用户必须滑动然后按“删除”按钮。

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    return nil
}
编辑:在iOS 11 / XCode 9 / Swift 4更新之前,我已经实现了canEditRowAt并提交了编辑样式。即使在我实现了trailingSwipeActionsConfigurationForRowAt之前,也启用了完全滑动。

2 个答案:

答案 0 :(得分:20)

实施如下:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
        print("index path of delete: \(indexPath)")
        completionHandler(true)
    }
    let swipeAction = UISwipeActionsConfiguration(actions: [delete])
    swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
    return swipeAction
}

这是禁用完全滑动的行

swipeAction.performsFirstActionWithFullSwipe = false 

如果您实施editingStyleeditActionsForRowAt之类的其他功能,请删除其他功能。

答案 1 :(得分:1)

我可以按照以下答案为特定单元格禁用滑动操作: https://stackoverflow.com/a/50597672/1072262

您可以返回以下内容,而不是返回nil:

return UISwipeActionsConfiguration.init()