iOS开发 自定义tableviewCell、自动变高、动态布局、删除按钮替换图片和文字

在iOS系统自带的tableviewCell 中,可以实现滑动删除,但是不能更改删除的文字、也不能换成图片、但是可以改背景,下面来实现一下 自定义的cell 实现删除按钮  替换图片和文字


具体做法是  在cell 上加一个button,当button加上去之后就可以实现文字和图片的替换了,然后在在上面加上左滑和右滑手势,左滑的时候显示  删除按钮、右滑的时候在华回去同时再加上动画,就和系统的差不多了,废话不多说,上代码吧:


#import <UIKit/UIKit.h>
#import "MSignMdell.h"

@interface MSignTableViewCell : UITableViewCell<UIGestureRecognizerDelegate>

@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *messageLabel;
@property (nonatomic,weak) UIButton *deletBtn;
@property (nonatomic, copy) MSignMdell *signModel;

@property (nonatomic, assign) id delegate;

@property (nonatomic,copy) void (^deleBlock)(void);

@property (nonatomic,copy) void (^swpLeftBlock)(void);
@property (nonatomic,copy) void (^swpRightBlock)(void);

@property (nonatomic,copy) void (^clickBlock)(void);

@end




#import "MSignTableViewCell.h"
#import "MModelViewController.h"

@implementation MSignTableViewCell

- (void)awakeFromNib {
    // Initialization code
//    [self configureView];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}


-(void)configureView
{
    
    self.contentView.backgroundColor = [UIColor clearColor];
    
    //    图标
    self.iconImgView = [self creatImgViewWithFrame:CGRectMake(20, 10, 15, 15) img:[UIImage imageNamed:@"icon_grqm_zsd"] supperView:self.contentView];
    self.iconImgView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.iconImgView];
    //    签名 时间
    self.timeLabel = [self creatLabelWithFrame:CGRectMake(self.iconImgView.frame.size.width + self.iconImgView.frame.origin.x + 10, 8, 200, 18) text:@"时间" font:13.0 supperView:self.contentView];
    self.timeLabel.textAlignment = NSTextAlignmentLeft;
    self.timeLabel.backgroundColor = [UIColor clearColor];
    
    //    签名  内容
    self.messageLabel = [self creatLabelWithFrame:CGRectMake(self.iconImgView.frame.size.width + self.frame.origin.x + 16, self.timeLabel.frame.size.height + self.timeLabel.frame.origin.y + 5, self.frame.size.width - self.iconImgView.frame.size.width - self.iconImgView.frame.origin.x - 20, 20) text:@"内容" font:13.0 supperView:self.contentView];
    self.messageLabel.textAlignment = NSTextAlignmentLeft;
    self.messageLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    self.messageLabel.numberOfLines = 0;
    self.messageLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_grqm_bg"]];
    
    self.deletBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.deletBtn.frame = CGRectMake(self.messageLabel.frame.origin.x + self.messageLabel.frame.size.width, 0, 40, self.contentView.frame.size.height);
    [self.deletBtn setBackgroundImage:[UIImage imageNamed:@"icon_grqm_trash"] forState:UIControlStateNormal];
    
    [self.deletBtn addTarget:self action:@selector(deletBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.deletBtn];
    
    UISwipeGestureRecognizer *swpLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swpLeftBlock:)];
    [swpLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.contentView addGestureRecognizer:swpLeft];
    swpLeft.delegate = self;
    
    UISwipeGestureRecognizer *swpRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swpRight:)];
    //    swp.description UISwipeGestureRecognizerDirectionLeft;
    [swpRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.contentView addGestureRecognizer:swpRight];
    swpRight.delegate = self;
    
    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOne:)];
    tapOne.delegate = self;
    tapOne.numberOfTapsRequired = 1;
    tapOne.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:tapOne];
    
}

- (void)deletBtnClick:(UIButton *)btn
{
    if (self.deleBlock) {
        self.deleBlock();
    }
}

-(void)swpLeftBlock:(UISwipeGestureRecognizer *)swp
{
    if (self.swpLeftBlock) {
        self.swpLeftBlock();
    }
}

-(void)swpRight:(UISwipeGestureRecognizer *)swp
{
    if (self.swpRightBlock) {
        self.swpRightBlock();
    }
}

- (void)tapOne:(UITapGestureRecognizer *)tap
{
    if (self.clickBlock) {
        self.clickBlock();
    }
}


-(void)setSignModel:(MSignMdell *)signModel
{
    if (_signModel != signModel) {
        _signModel = signModel;
    }
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@",_signModel.timeStr];
    self.messageLabel.text = [NSString stringWithFormat:@"%@",_signModel.signStr];

    UIFont *font = [UIFont systemFontOfSize:13.0];
    self.messageLabel.font = font;
    // 根据字体得到NSString的尺寸

    CGSize size = [self.messageLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(self.messageLabel.frame.size.width, 100) lineBreakMode:NSLineBreakByClipping];
    [self.messageLabel setFrame:CGRectMake(10, self.messageLabel.frame.origin.y, size.width, size.height)];
//    self.messageLabel.backgroundColor = [UIColor redColor];
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.timeLabel.frame.size.height + self.timeLabel.frame.origin.y + self.messageLabel.frame.size.height + self.messageLabel.frame.origin.y - 20);
}

#pragma mark
#pragma mark 控件封装
-(UIImageView *)creatImgViewWithFrame:(CGRect)frame img:(UIImage *)img supperView:(UIView *)supperView
{
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:frame];
    imgView.image = img;
    imgView.userInteractionEnabled = YES;
    [supperView addSubview:imgView];
    return imgView;
}

-(UILabel *)creatLabelWithFrame:(CGRect)frame text:(NSString *)text font:(CGFloat)font supperView:(UIView *)supperView
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = text;
    //    label.backgroundColor = [UIColor redColor];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:font];
    [supperView addSubview:label];
    return label;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

这样就好了,然后在控制器里面就可以调用了,效果不错,这里没加动画效果




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值