美文网首页iOS视图
iOS~ 自定义:cell左滑删除按钮样式、上下移动位置

iOS~ 自定义:cell左滑删除按钮样式、上下移动位置

作者: 阳光下的叶子呵 | 来源:发表于2022-04-01 12:23 被阅读0次

1、左滑删除cell:

#pragma mark 左滑删除cell
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.oftenModels.count > 0) {
        // 类型: 1 :添加的球场 2:上次选择 3 :离我最近
        NSInteger type = self.oftenModels[indexPath.row].type;

        if (type == 1) {
                   
//            [self changeConfigSwipeButtons];
            
            //删除
            UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                
                /**   点击 删除 按钮的操作 */
                if (self.deleteSelectCellBlock) {
                    self.deleteSelectCellBlock(YES, indexPath.row);
                }
            }];
            
            
            deleteRowAction.title = @"删除";
//            deleteRowAction.backgroundColor = RGBA(223, 109, 53, 1);
//            deleteRowAction.backgroundColor = [UIColor clearColor];
            
            UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
            config.performsFirstActionWithFullSwipe = NO; // 禁止侧滑无线拉伸
            
            return config;
        } else {
            return nil;
        }
    } else {
        return nil;
    }
    
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"😭😭 == willBeginEditing");
    [self changeConfigSwipeButtons];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"😆😆😆😆 == didEndEditing");
}


//- (void)layoutSubviews {
//    [super layoutSubviews];
////    [self changeConfigSwipeButtons];
//}

#pragma mark -- -- 自定义修改左滑 删除按钮 样式
- (void)changeConfigSwipeButtons {

    // iOS 11层级 : UITableView -> UISwipeActionPullView(当左滑出现“删除”按钮时,按钮的俯视图是UISwipeActionPullView)
    for (UIView *subview in self.tableView.subviews) {
         // ios 11以下
         if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 1)
         {
              UIButton*readButton = subview.subviews[0];
              [readButton setImage:[UIImage imageNamed:@"plan_delete"] forState:UIControlStateNormal];
              break;
         }
        // ios 13
        // _UITableViewCellSwipeContainerView 是tableview 的子视图,UISwipeActionPullView是_UITableViewCellSwipeContainerView的子视图
         else if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subview.subviews count] >= 1) {
             for (UIView *subview0 in subview.subviews){
                 if ([subview0 isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview0.subviews count] >= 1){
                     
                     UIButton *deleteButton = subview0.subviews[0]; // 第一层视图
                     deleteButton.layer.masksToBounds = YES;
                     deleteButton.layer.cornerRadius = 12;
                     deleteButton.layer.backgroundColor = RGBA(223, 109, 53, 1).CGColor;
                     [deleteButton setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(@"de6d3c") size:CGSizeMake(80, 135)] forState:UIControlStateNormal];
                     
//                     deleteButton.backgroundColor = RGBA(223, 109, 53, 1);
//                     [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
//                     [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//                     deleteButton.titleLabel.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*16 weight:UIFontWeightMedium];
//                     deleteButton.layer.masksToBounds = YES;
//                     deleteButton.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*12;
//                     [deleteButton makeConstraints:^(MASConstraintMaker *make) {
//                         make.center.equalTo(subview0.subviews[0]);
//                         make.width.mas_equalTo(200);
//                         make.height.mas_equalTo(30);
//                     }];
                     
                     
                     //修改按钮-颜色
//                     UIButton *swipeActionStandardBtn = subview0.subviews[0];
//                     swipeActionStandardBtn.backgroundColor = RGBA(223, 109, 53, 1);
//                     swipeActionStandardBtn.layer.masksToBounds = YES;
//                     swipeActionStandardBtn.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*12;
//                     swipeActionStandardBtn.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner; // 设置某一个角(自动布局可用)
//                     swipeActionStandardBtn.clipsToBounds = YES; // 设置这个属性,子视图超出边界裁剪掉
//                     if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
//                             CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
//                             CGFloat swipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;
//                             swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 0, swipeActionStandardBtnW, 30);
//                     }
                     
                     
                     break;
                 }
             }
         }

    }
}


+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

2、UITableView上下移动位置(系统):

在UITableView中,我们可以使用- (BOOL) tableView: (UITableView *) tableView canMoveRowAtIndexPath: (NSIndexPath *) indexPath方法来禁止移动某一行。下面的例子是禁止移动最后一行。但是,虽然不能移动最后一行,却可以将其他行移动至最后一行下方。
所以需要方法:- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;来设置。

[self.tableView setEditing:YES animated:YES]; // 进入可编辑状态
//默认编辑模式下,每个cell左边有个红色的删除按钮,设置为None即可去掉
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

//是否允许indexPath的cell移动
//是否允许indexPath的cell移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { // 禁止第一行移动
        return NO;
    } else {
        return YES;
    }
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"移动的位置索引 = %ld,\n 将要移动到达的位置索引 == %ld", sourceIndexPath.row, destinationIndexPath.row);
    //更新数据源
}

#pragma mark -- 禁止某一行移动
// 禁止某一行移动,并且禁止其他cell移动到该cell的索引位置(这里禁止移动第一行)
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    
    NSIndexPath *indexPath = nil;
    
    // 要求:第一个cell位置置顶,不可移动,所以当移动其他cell到该位置时,进行下列操作:
    if (proposedDestinationIndexPath.row == 0) {
        indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
    } else {
        indexPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row inSection: 0];
    }
    return indexPath;
}

// 拖拽 预览
- (nullable UIDragPreviewParameters *)tableView:(UITableView *)tableView dragPreviewParametersForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return nil;
    
    // 可以在该方法内使用 贝塞尔曲线 对单元格的一个具体区域进行裁剪
    UIDragPreviewParameters *parameters = [[UIDragPreviewParameters alloc] init];

    CGFloat previewLength = self.tableView.frame.size.width;
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/375*(335), [UIScreen mainScreen].bounds.size.width/375*(75 + 12));
    parameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:[UIScreen mainScreen].bounds.size.width/375*16];
    parameters.backgroundColor = RGBA(248, 249, 250, 1);
    return parameters;
}

- (void)tableView:(UITableView *)tableView dragSessionWillBegin:(id<UIDragSession>)session {
    // 将要 拖拽
    NSLog(@"😁😆");
}

- (void)tableView:(UITableView *)tableView dragSessionDidEnd:(id<UIDragSession>)session {
    // 拖拽 结束
    NSLog(@"😁😆");
}

相关文章

网友评论

    本文标题:iOS~ 自定义:cell左滑删除按钮样式、上下移动位置

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