编辑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<UITableViewRowAction *> *)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;
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容