美文网首页
iOS 左滑cell删除功能

iOS 左滑cell删除功能

作者: KingWorld | 来源:发表于2021-05-21 16:55 被阅读0次

    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)

    相关文章

      网友评论

          本文标题:iOS 左滑cell删除功能

          本文链接:https://www.haomeiwen.com/subject/mzwwjltx.html