[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;
}