UITableView自定义cell

2020-06-15  本文已影响0人  ly_chee_

.h文件内容

#import

@class RedModel;

NS_ASSUME_NONNULL_BEGIN

typedefvoid(^ButtonClick)(UIButton* sender);

@interface SquareImageCell : UITableViewCell

@property (nonatomic,strong) RedModel * model;

@property (nonatomic,copy) ButtonClick followAction;

@end

NS_ASSUME_NONNULL_END

.m文件内容

#import "SquareImageCell.h"

#import "RedModel.h"

@interface SquareImageCell ()

@property (nonatomic,strong) UIImageView *headImgView;

@property (nonatomic,strong) UILabel *nameLabel;

@property (nonatomic,strong) UIButton * followBtn;

@end

@implementation SquareImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        _headImgView = [[UIImageView alloc] init];

        _headImgView.layer.cornerRadius = 20;

        _headImgView.layer.masksToBounds = YES;

        [self.contentView addSubview:_headImgView];

         _headImgView.frame = CGRectMake(20, 0,  50,  50);

        _nameLabel = [[UILabel alloc] init ];

        _nameLabel.font = [UIFont systemFontOfSize:14];

        _nameLabel.textColor = UIColorFromRGB(0x333333, 1);

        [self.contentView addSubview:_nameLabel];

        _nameLabel.frame = CGRectMake(100, 0,  220,  50);

        _followBtn = [[UIButton alloc] initWithFrame:CGRectMake(260, 0, 42, 50)];

        [_followBtn setTitle:@"关注" forState:UIControlStateNormal];

        _followBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        [_followBtn addTarget:self action:@selector(followClick:) forControlEvents: UIControlEventTouchUpInside];

        [_followView addSubview:_followBtn];

        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];

        lineView.backgroundColor = UIColorFromRGB(0xF8F8F8, 1);

        [self.contentView addSubview:lineView ];

    }

    return self;

}

#pragma mark- Action

- (void)followClick:(UIButton*)button{

// 判断下这个block在控制其中有没有被实现

    if (self.followAction) {

// 调用block传入参数

        self.followAction(button);

    }

}

#pragma mark- setModel

-(void)setModel:(RedModel*)model{

    _headImgView.image = [UIImage imageNamed:model.imgUrl];

    _nameLabel.text = model.title;

 }

@end

上一篇 下一篇

猜你喜欢

热点阅读