iOS11下UITableView侧滑删除详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/83754777

说到编辑模式我们一般会用这个.更老的基本上没人会用到了.所以就不提了

// Use -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: instead of this method, which will be deprecated in a future release.
// This method supersedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

但是在iOS11上.侧滑返回多了一个可以滑动到顶然后触发第一个action的功能.那么,我们如何才能禁止这个侧滑删除呢.
其实看我粘贴过来的注释也十分的明了了.

注意下面这个方法是iOS11以上支持的

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

下面是使用举例
这是之前咱们的代码

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"action1" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

    }];
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"action2" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

    }];
    return @[action1 ,action2];
}

侧滑到顶的时候肯定是激活了action1

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) {
    UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"action1" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {

    }];
    UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"action2" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {

    }];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[action1 ,action2]];
    config.performsFirstActionWithFullSwipe = NO;
    return config;
}

当设置performsFirstActionWithFullSwipe为YES的时候就是允许侧滑删除.当设置为NO的时候只能单击Action来删除

到这里就结束了么?没,我们看看他旁边的一个方法

// 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);

呃,如果实现了这个方法,如果return nil的话就是默认的侧滑的action.
这个方法和上面的方法唯一的不同就是trailingleading.trailing是在后头拉出,也就是11之前的那种默认编辑模式.trailing是前面拉出.也就是iOS11新出的前面拉动.也就是从左往右拉出cell左边的action.

因为是iOS11支持的API.所以旧的建议也写上(工程支持在iOS11以上的就无视我建议吧)

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/83754777