iOS 问答集合搬砖

iOS11 如何解决UITableView侧滑删除无限拉伸

2018-06-28  本文已影响73人  船长_
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
}

iOS11如果仍然用以上方法处理侧滑删除功能,但是会发现侧滑无限拉伸,然后自动调用删除方法

iOS11新增了方法

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){
    if (@available(iOS 11.0, *)) {
        
        NSString *title = @"置顶";
        if (indexPath.section == 0) {
            title = @"取消置顶";
        } else {
            title = @"置顶";
        }
        UIContextualAction *topAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:title handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            
           ...
            
            // 这句很重要,退出编辑模式,隐藏左滑菜单
            [tableView setEditing:NO animated:YES];
            completionHandler(true);
        }];
        
        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            
           // 这句很重要,退出编辑模式,隐藏左滑菜单
            [tableView setEditing:NO animated:YES];
            completionHandler(true);
        }];
        
        UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,topAction]];
        // 禁止侧滑无线拉伸
        actions.performsFirstActionWithFullSwipe = NO;
        return actions;
    }else{
        return nil;
    }
}

iOS11之前,并不会发生侧滑无线拉伸

上一篇下一篇

猜你喜欢

热点阅读