iOS 左滑cell删除功能

2021-05-21  本文已影响0人  KingWorld

UITableViewDelegate上写

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        [self customDeleteBtnAfteriOS11:tableView];
    }
}

- (void)customDeleteBtnAfteriOS11:(UITableView *)tableView {
    for (UIView *subview in tableView.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
            //设置按钮frame
            for (UIView * sonView in subview.subviews) {
                if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    sonView.frame = CGRectMake(-15, 0, 70, 70);
                    sonView.backgroundColor = KLColor(255, 110, 110);
                }
            }
        }
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath {
    KLWeakSelf
    if (@available(iOS 11.0, *)) {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *_Nonnullaction, NSIndexPath *_NonnullindexPath) {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除?" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf didClickDelete:self.listArrayM[indexPath.row] Ftype:KLEditPatientTypeDelete];
        }]];
        
        [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        
        [self presentViewController:alertVc animated:YES completion:nil];
        
    }];

    rowAction.backgroundColor = [UIColor clearColor];
    return @[rowAction];
    }else {
        UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *_Nonnullaction, NSIndexPath *_NonnullindexPath) {
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除?" preferredStyle:UIAlertControllerStyleAlert];
            
            [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [weakSelf didClickDelete:self.listArrayM[indexPath.row] Ftype:KLEditPatientTypeDelete];
            }]];
            
            [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
            
            [self presentViewController:alertVc animated:YES completion:nil];
            
        }];
        
        rowAction.backgroundColor = [UIColor clearColor];
        return @[rowAction];
    }
}

参考: UITableView左滑删除自定义(支持ios11)

上一篇下一篇

猜你喜欢

热点阅读