使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)...
生活随笔
收集整理的這篇文章主要介紹了
使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼:
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]; }轉載于:https://www.cnblogs.com/xuzb/p/8706737.html
總結
以上是生活随笔為你收集整理的使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【AI芯片格局最全分析】国内AI芯片百家
- 下一篇: 23种设计模式之《单例模式》