使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)
程序员文章站
2022-04-19 17:37:59
直接上代码: 1:先自定义cell .h文件中 .m文件中 2:在控制器.m文件中使用 ......
直接上代码:
1:先自定义cell
.h文件中
#import <UIKit/UIKit.h> #import "LBDNewMsgListModel.h" #import "MLEmojiLabel.h" @interface LBDMsgCenterCell : UITableViewCell<MLEmojiLabelDelegate, UIGestureRecognizerDelegate> @property (nonatomic, strong) UILabel *msgDateLbl; @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIView *topLineView; @property (nonatomic, strong) UIImageView *msgImgView; @property (nonatomic, strong) UILabel *titleLbl; @property (nonatomic, strong) UILabel *orderNum; @property (nonatomic, strong) UIView *lineView; @property (nonatomic, strong) UILabel *lookLbl; @property (nonatomic, strong) MLEmojiLabel *contentLbl; @property (nonatomic, strong) LBDNewMsgListModel *model; @end
.m文件中
#import "LBDMsgCenterCell.h" #import "LBDNewMsgListModel.h" #import "NSDate+Extend.h" @implementation LBDMsgCenterCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self createView]; [self setSubviews]; } return self; } #pragma make 创建子控件 - (void)createView { _msgDateLbl = [[UILabel alloc] init]; _msgDateLbl.font = [UIFont systemFontOfSize:10]; _msgDateLbl.textColor = [UIColor colorWithHex:@"646464"]; _msgDateLbl.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_msgDateLbl]; _bgView = [[UIView alloc] init]; _bgView.backgroundColor = [UIColor whiteColor]; _bgView.layer.masksToBounds = YES; _bgView.layer.cornerRadius = 10; [self.contentView addSubview:_bgView]; _msgImgView = [[UIImageView alloc] init]; _msgImgView.hidden = YES; _msgImgView.contentMode = UIViewContentModeScaleToFill; [_bgView addSubview:_msgImgView]; _topLineView = [[UIView alloc] init]; _topLineView.hidden = YES; [_bgView addSubview:_topLineView]; _titleLbl = [[UILabel alloc] init]; _titleLbl.font = [UIFont systemFontOfSize:15]; _titleLbl.numberOfLines = 0; _titleLbl.textColor = [UIColor colorWithHex:@"505050"]; [_bgView addSubview:_titleLbl]; _lookLbl = [[UILabel alloc] init]; _lookLbl.textAlignment = NSTextAlignmentRight;; _lookLbl.text = @"立即查看"; _lookLbl.font = [UIFont systemFontOfSize:13]; [_bgView addSubview:_lookLbl]; _lineView = [[UIView alloc] init]; _lineView.backgroundColor = [UIColor unclickableColor]; [_bgView addSubview:_lineView]; [_bgView addSubview:self.contentLbl]; } - (void)setModel:(LBDNewMsgListModel *)model { NSDateFormatter *formater = [[NSDateFormatter alloc] init]; formater.dateFormat = @"yyyy/MM/dd HH:mm"; NSDate *sendDate = [formater dateFromString:model.createdAt]; if ([sendDate isToday]) { _msgDateLbl.text = [NSString stringWithFormat:@"今天 %@", [model.createdAt timestampTransformDateToType:@"HH:mm"]]; } else if ([sendDate isYesToday]) { _msgDateLbl.text = [NSString stringWithFormat:@"昨天 %@", [model.createdAt timestampTransformDateToType:@"HH:mm"]]; } else { _msgDateLbl.text = [model.createdAt timestampTransformDateToType:@"yyyy/MM/dd HH:mm"]; } _titleLbl.text = model.title; self.contentLbl.text = model.content; [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(_msgDateLbl.mas_bottom); make.left.mas_equalTo(self.contentView.mas_left).offset(10); make.right.mas_equalTo(self.contentView.mas_right).offset(-10); make.bottom.mas_equalTo(self.contentView.mas_bottom); }]; if ([model.colorType isEqualToString:@"1"]) { _topLineView.hidden = NO; _msgImgView.hidden = YES; [_msgImgView sd_setImageWithURL:nil]; _topLineView.backgroundColor = [UIColor redColor]; [_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left); make.top.mas_equalTo(_bgView.mas_top); make.right.mas_equalTo(_bgView.mas_right); make.height.mas_equalTo(5); }]; [_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left).offset(10); make.right.mas_equalTo(_bgView.mas_right).offset(-10); make.top.mas_equalTo(_topLineView.mas_bottom).offset(6); }]; [self.contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_titleLbl.mas_left); make.right.mas_equalTo(_titleLbl.mas_right); make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8); }]; } else if ([model.colorType isEqualToString:@"2"]) { _topLineView.hidden = NO; _msgImgView.hidden = YES; [_msgImgView sd_setImageWithURL:nil]; _topLineView.backgroundColor = [UIColor colorWithHex:@"00ade5"]; [_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left); make.top.mas_equalTo(_bgView.mas_top); make.right.mas_equalTo(_bgView.mas_right); make.height.mas_equalTo(5); }]; [_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left).offset(10); make.right.mas_equalTo(_bgView.mas_right).offset(-10); make.top.mas_equalTo(_topLineView.mas_bottom).offset(6); }]; [self.contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_titleLbl.mas_left); make.right.mas_equalTo(_titleLbl.mas_right); make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8); }]; } else if ([model.colorType isEqualToString:@"3"]) { _topLineView.hidden = YES; _msgImgView.hidden = NO; [_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left); make.top.mas_equalTo(_msgDateLbl.mas_bottom); make.right.mas_equalTo(_bgView.mas_right); make.height.mas_equalTo(0); }]; [_msgImgView sd_setImageWithURL:[NSURL URLWithString:model.activityImg] placeholderImage:[UIImage imageNamed:@"PersonalInformationImagePlaceholder"]]; [_msgImgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left); make.right.mas_equalTo(_bgView.mas_right); make.top.mas_equalTo(_msgDateLbl.mas_bottom); make.height.mas_equalTo(SCREEN_WIDTH / 3.41); }]; [_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left).offset(10); make.right.mas_equalTo(_bgView.mas_right).offset(-10); make.top.mas_equalTo(_msgImgView.mas_bottom).offset(6); }]; [_contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_titleLbl.mas_left); make.right.mas_equalTo(_titleLbl.mas_right); make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8); }]; } if ([model.jump isEqualToString:@"0"]) { [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.contentLbl.mas_left); make.right.mas_equalTo(self.contentLbl.mas_right); make.height.mas_equalTo(0); make.top.mas_equalTo(self.contentLbl.mas_bottom); }]; [_lookLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.contentLbl.mas_right); make.width.mas_equalTo(80); make.height.mas_equalTo(0); make.top.mas_equalTo(_lineView.mas_bottom); make.bottom.mas_equalTo(_bgView.mas_bottom); }]; } else if ([model.jump isEqualToString:@"1"]) { [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.contentLbl.mas_left); make.right.mas_equalTo(self.contentLbl.mas_right); make.height.mas_equalTo(0.5); make.top.mas_equalTo(self.contentLbl.mas_bottom); }]; [_lookLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.contentLbl.mas_right); make.width.mas_equalTo(100); make.height.mas_equalTo(30); make.top.mas_equalTo(_lineView.mas_bottom); make.bottom.mas_equalTo(_bgView.mas_bottom); }]; } } - (void)setSubviews { [_msgDateLbl mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.contentView.mas_left); make.top.mas_equalTo(self.contentView.mas_top); make.right.mas_equalTo(self.contentView.mas_right); make.height.mas_equalTo(30); }]; } #pragma mark - delegate - (void)mlEmojiLabel:(MLEmojiLabel*)emojiLabel didSelectLink:(NSString*)link withType:(MLEmojiLabelLinkType)type { switch(type){ case MLEmojiLabelLinkTypeURL: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]]; break; case MLEmojiLabelLinkTypePhoneNumber: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", link]]]; break; case MLEmojiLabelLinkTypeEmail: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", link]]]; break; case MLEmojiLabelLinkTypeAt: NSLog(@"点击了用户%@",link); break; case MLEmojiLabelLinkTypePoundSign: NSLog(@"点击了话题%@",link); break; default: NSLog(@"点击了不知道啥%@",link); break; } } - (MLEmojiLabel *)contentLbl { if (!_contentLbl) { _contentLbl = [MLEmojiLabel new]; _contentLbl.numberOfLines = 0; _contentLbl.font = [UIFont systemFontOfSize:13.0f]; _contentLbl.delegate = self; _contentLbl.backgroundColor = [UIColor whiteColor]; _contentLbl.lineBreakMode = NSLineBreakByTruncatingTail; _contentLbl.textColor = [UIColor colorWithHex:@"a0a0a0"]; _contentLbl.disableEmoji = NO; _contentLbl.lineSpacing = 3.0f; _contentLbl.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter; } return _contentLbl; }
2:在控制器.m文件中使用
- (void)drawView { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.backgroundColor = [UIColor bgColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.delegate = self; _tableView.dataSource = self; [self addSubview:_tableView]; // 给一个标识符,告诉tableView要创建哪个类 [_tableView registerClass:[LBDMsgCenterCell class] forCellReuseIdentifier:@"LBDMsgCenterCell"]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.tableView fd_heightForCellWithIdentifier:@"LBDMsgCenterCell" cacheByIndexPath:indexPath configuration:^(LBDMsgCenterCell *cell) { // 在这个block中,重新cell配置数据源 [self setupModelOfCell:cell atIndexPath:indexPath]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _msgArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LBDMsgCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LBDMsgCenterCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor clearColor]; [self setupModelOfCell:cell atIndexPath:indexPath]; return cell; } - (void)setupModelOfCell:(LBDMsgCenterCell *) cell atIndexPath:(NSIndexPath *) indexPath { // 采用计算frame模式还是自动布局模式,默认为NO,自动布局模式 // cell.fd_enforceFrameLayout = NO; cell.model = _msgArr[indexPath.row]; }