编辑tableview, editActionsForRowAtIndexPath

// 可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentIndex < 2) {
        return YES;
    }
    return NO;
}

// 设置进入编辑状态 cell不会缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 添加action
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // iOS系统小于11则使用常规方法
    if(iOS_VERSION.floatValue < 11.0) {
        __weak __typeof__(self) weakSelf = self;
        
        // 历史数据和标准数据
        if (self.currentIndex < 2) {
            
            // 删除一条
            UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                __strong FileChildViewController *strongSelf = weakSelf;
                [strongSelf deleteOne:indexPath];
            }];
            deleteAction.backgroundColor = BaseRedColor;
            
            // 上传一条
            UITableViewRowAction *uploadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"上传" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                __strong FileChildViewController *strongSelf = weakSelf;
                [strongSelf uploadOne:indexPath];
            }];
            
            if (self.currentIndex == 0) {
                // 设置为标准数据
                UITableViewRowAction *savetoStandardAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"设置为标准数据") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                    __strong FileChildViewController *strongSelf = weakSelf;
                    [strongSelf saveToStandard:indexPath];
                }];
                [savetoStandardAction setBackgroundColor:[UIColor blueColor]];
                return @[deleteAction,uploadAction,savetoStandardAction];
            } else {
                // 设置为对比数据
                UITableViewRowAction *comparisonAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"设为对比数据") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                    __strong FileChildViewController *strongSelf = weakSelf;
                    [strongSelf setToComparision:indexPath];
                }];
                [comparisonAction setBackgroundColor:[UIColor blueColor]];
                return @[deleteAction,uploadAction,comparisonAction];
            }
        }
        return nil;
    } else {
        return nil;
    }
}

// 只适用于ios11 以及以后版本
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)) {
    if (@available(iOS 11.0, *)) {
        if (self.currentIndex < 2) {
            
            __weak __typeof__(self) weakSelf = self;
            // 删除数据
            UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
                __strong FileChildViewController *strongSelf = weakSelf;
               [strongSelf deleteOne:indexPath];
                completionHandler(YES);
            }];
            [deleteAction setBackgroundColor:BaseRedColor];
            
            // 上传数据
            UIContextualAction *uploadAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"上传" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
                __strong FileChildViewController *strongSelf = weakSelf;
                [strongSelf uploadOne:indexPath];
                completionHandler(YES);
            }];
            
            if (self.currentIndex == 0) {
                // 设为标准数据
                UIContextualAction *standardAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"设置为标准数据") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                    [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
                    __strong FileChildViewController *strongSelf = weakSelf;
                    [strongSelf saveToStandard:indexPath];
                    completionHandler(YES);
                }];
                [standardAction setBackgroundColor:[UIColor blueColor]];
                // 添加
                UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,uploadAction,standardAction]];
                actions.performsFirstActionWithFullSwipe = NO;
                return actions;
            } else {
                // 设置为对比数据
                UIContextualAction *comparisionAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"设为对比数据") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                    [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
                    __strong FileChildViewController *strongSelf = weakSelf;
                    [strongSelf setToComparision:indexPath];
                    completionHandler(YES);
                }];
                [comparisionAction setBackgroundColor:[UIColor blueColor]];
                // 添加
                UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,uploadAction,comparisionAction]];
                actions.performsFirstActionWithFullSwipe = NO;
                return actions;
            }
            
        } else {// 云端数据暂时无
            return nil;
        }
    }
    return nil;
}

你可能感兴趣的:(编辑tableview, editActionsForRowAtIndexPath)