html 自定义tableview,iOS新手开发之旅-纯代码自定义tableViewCell、自适应高度

[Objective-C] 纯文本查看 复制代码#import "FruitNewCell.h"

@implementation FruitNewCell

+ (CGFloat)getCellHeightByDesc:(NSString *)strDesc{

return 5 + 20 + [strDesc boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;

}

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

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

self.selectionStyle = UITableViewCellSelectionStyleNone;

if (!_imgIcon) {

_imgIcon = [UIImageView new];

_imgIcon.frame = CGRectMake(8, 5, 40, 40);

[self.contentView addSubview:_imgIcon];

}

if (!_lblName) {

_lblName = [UILabel new];

_lblName.frame = CGRectMake(CGRectGetMaxX(_imgIcon.frame), CGRectGetMinX(_imgIcon.frame), 150, 20);

_lblName.font = [UIFont systemFontOfSize:14];

[self.contentView addSubview:_lblName];

}

if (!_lblDesc) {

_lblDesc = [UILabel new];

_lblDesc.frame = CGRectMake(CGRectGetMinX(_lblName.frame), CGRectGetMaxY(_lblName.frame), CGRectGetWidth(_lblName.frame), CGRectGetHeight(_lblName.frame));

_lblDesc.font = [UIFont systemFontOfSize:13];

_lblDesc.numberOfLines = 0;

_lblDesc.textColor = [UIColor lightGrayColor];

[self.contentView addSubview:_lblDesc];

}

}

return self;

}

//重写set方法

-(void)setFruitModel:(FruitModel *)fruitModel{

_fruitModel = fruitModel;

self.lblName.text = [NSString stringWithFormat:@"%@(%@)",fruitModel.name,fruitModel.egname];

self.imgIcon.image = [UIImage imageNamed: fruitModel.icon];

self.lblDesc.text = fruitModel.desc;

CGFloat descHeight = [fruitModel.desc boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;

CGRect frame = self.lblDesc.frame;

frame.size.height = descHeight;

self.lblDesc.frame = frame;

}

+ (instancetype)fruitCellWithTableView:(UITableView *)tableView{

static NSString *ID = @"fruit_cell";

FruitNewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[FruitNewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//不能加xib后缀

}

return cell;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值