iOS tableViewCell 自定义左滑删除按钮样式的2种方法

第一种方法

#pragma mark - UITableView滑动删除

// 先要设Cell可编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 添加一个删除按钮

    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        NSLog(@"点击了删除");

    }];

//

//   // deleteRowAction.backgroundColor = [UIColor blackColor];

//

//    // 删除一个置顶按钮

//    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

//        NSLog(@"点击了置顶");

//    }];

//    topRowAction.backgroundColor = [UIColor blueColor];

//    // 添加一个更多按钮

//    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

//        NSLog(@"点击了更多");

//

//    }];

//    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

//    // 将设置好的按钮放到数组中返回return @[deleteRowAction, topRowAction, moreRowAction];

    return @[deleteRowAction];

}

其中不修改backgroundColor时,backgroundColor的颜色是由style决定的,UITableViewRowActionStyleDestructive时是红色的删除样式,UITableViewRowActionStyleNormal时是灰色样式,类似于微信好友列表左划后的“备注”。
 

第二种方法

iOS11之前

for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            for (UIButton *btn in subView.subviews) {
                if ([btn isKindOfClass:[UIButton class]]) {
/*在此处可以自定义删除按钮的样式*/
                    btn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
                }
            }
        }
    }

将上边⤴️代码加入到 自定义cell.m 文件 layoutSubviews 方法中!!!之外还要设置tableview的代理方法

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}
//Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//设置进入编辑状态时,Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.arr removeObjectAtIndex:indexPath.row];
    [self.mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

或者采用下面的方法

// 改变滑动删除按钮样式

- (void)layoutSubviews {

    [super layoutSubviews];

    for (UIView *subView in self.subviews){

        if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {

            CGRect cRect = subView.frame;

            //删除按钮显示范围

            cRect.size.height = self.contentView.frame.size.height - 10;

            subView.frame = cRect;

            

            UIView *confirmView=(UIView *)[subView.subviews firstObject];

            // 改背景颜色

           // confirmView.backgroundColor=[UIColor greenColor];

       //侧滑设置图片

            UIImage *image = [UIImage imageNamed:@"lufei"];

            confirmView.layer.contents = (id)image.CGImage;

            for(UIView *sub in confirmView.subviews){

                if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){

                    UILabel *deleteLabel=(UILabel *)sub;

                    // 改删除按钮的字体

                    deleteLabel.font=[UIFont boldSystemFontOfSize:15];

                    // 改删除按钮的文字

                    deleteLabel.text=@"67";

                }

            }

            break;

        }

    }

}

iOS tableViewCell 自定义左滑删除按钮样式的2种方法_第1张图片

 

 

补充:(iOS 11 之后)

  • 只需要设置tableview一个代理方法可以更加方便的实现以上功能                                                                      

#pragma mark 测试
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    //删除
    if (@available(iOS 11.0, *)) {
        UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            [self.arr removeObjectAtIndex:indexPath.row];
            completionHandler (YES);
            [self.mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
//    delete.image = [UIImage imageNamed:@"delete"];//这里还可以设置图片
    delete.backgroundColor = [UIColor grayColor];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    return config;
    } else {
        return nil;
        // Fallback on earlier versions
    }
}

 

  • cell加载动画

只要在代理方法中加入动画代码即可
 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.x = - SCREEN_WIDTH;
    cell.alpha = 0.1;
    [UIView animateWithDuration:0.8 animations:^{
        cell.x = 0;
        cell.alpha = 1;
    }completion:^(BOOL finish){
    
    }];
}

 

你可能感兴趣的:(iOS tableViewCell 自定义左滑删除按钮样式的2种方法)