編輯tableview, editActionsForRowAtIndexPath

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

// 設(shè)置進(jìn)入編輯狀態(tài) cell不會(huì)縮進(jìn)
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 添加action
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // iOS系統(tǒng)小于11則使用常規(guī)方法
    if(iOS_VERSION.floatValue < 11.0) {
        __weak __typeof__(self) weakSelf = self;
        
        // 歷史數(shù)據(jù)和標(biāo)準(zhǔn)數(shù)據(jù)
        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) {
                // 設(shè)置為標(biāo)準(zhǔn)數(shù)據(jù)
                UITableViewRowAction *savetoStandardAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"設(shè)置為標(biāo)準(zhǔn)數(shù)據(jù)") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                    __strong FileChildViewController *strongSelf = weakSelf;
                    [strongSelf saveToStandard:indexPath];
                }];
                [savetoStandardAction setBackgroundColor:[UIColor blueColor]];
                return @[deleteAction,uploadAction,savetoStandardAction];
            } else {
                // 設(shè)置為對比數(shù)據(jù)
                UITableViewRowAction *comparisonAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:LOCALIZATION(@"設(shè)為對比數(shù)據(jù)") 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;
            // 刪除數(shù)據(jù)
            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];
            
            // 上傳數(shù)據(jù)
            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) {
                // 設(shè)為標(biāo)準(zhǔn)數(shù)據(jù)
                UIContextualAction *standardAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"設(shè)置為標(biāo)準(zhǔn)數(shù)據(jù)") 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 {
                // 設(shè)置為對比數(shù)據(jù)
                UIContextualAction *comparisionAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:LOCALIZATION(@"設(shè)為對比數(shù)據(jù)") 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 {// 云端數(shù)據(jù)暫時(shí)無
            return nil;
        }
    }
    return nil;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容