iOS左滑插入多个按钮,按钮自定义颜色、图片、文字字体等

iOS8.0之后UITableView新添加了,左滑可以添加多个按钮的方法

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    NSMutableArray *array = [NSMutableArray array];
    //插入多个按钮,按钮位置排序0~4,从右向左
    for(int i=0; i<4; i++){
        __block NSInteger tag = i;
        UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"       " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            [weakSelf tapRowAction:indexPath.row type:tag];
        }];
        rowAction.backgroundColor = HEX_COLOR(@"afb6c1");
        [array addObject:rowAction];
    }
    
    return array;
}

- (void)tapRowAction:(NSInteger)row type:(NSInteger)type{
    if(type==0){
        [[[CommonUIAlert alloc] init] showCommonAlertView:self title:@"" message:@"是否删除该名片" cancelButtonTitle:@"取消" otherButtonTitle:@"确定" cancle:^{
        } confirm:^{
            [self.dataArray removeObjectAtIndex:row];
            [self.tableView beginUpdates];
            [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView endUpdates];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    }else if(type==1){
        CardGroupViewController *vc = [CommonMethod getVCFromNib:[CardGroupViewController class]];
        vc.isShowGroupList = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else if(type==2){
        NSString *str = [NSString stringWithFormat:@"tel:%@",@"15261173162"];
        UIWebView *callWebView = [[UIWebView alloc]init];
        [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebView];
    }else{
        [self showMessageView:[NSArray arrayWithObjects:@"15261173162", nil] title:@""];
    }
}


自定义左滑按钮,UITableViewRowAction,在你自定义的cell或者UITableViewCell中,重写-(void)layoutSubviews方法

- (void)layoutSubviews{
    for (UIView *subView in self.subviews) {
        if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            NSArray *array = @[@"icon_mp_dete",@"icon_mp_fz",@"icon_mp_tele",@"icon_mp_message"];
            for(int i=0; i<4; i++){
                UIView *btnView = subView.subviews[i];
                btnView.backgroundColor = HEX_COLOR(@"afb6c1");
                for (UIView *btn in btnView.subviews) {
                    UIImageView *imageview = [[UIImageView alloc] init];
                    imageview.contentMode = UIViewContentModeScaleAspectFit;
                    imageview.image = kImageWithName(array[i]);
                    imageview.frame = CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height);
                    [btn addSubview:imageview];
                }
            }
        }
    }
}


效果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值