关于tableview 在cell上展示标签,实现高度自动计算

1、首先先展示一下要实现的效果:


2、如果整个页面使用collectionView来布局就比较简单了,但是如果是tableview的cell上来实现这种效果的话,高度不是很好控制,我使用了tableview自动行高,然后再cell里赋值的时候进行masonry布局,这里要控制好不要重复创建标签,根据具体需求可以选择传入一次数据源,也可以每次创建标签的时候,把之前创建的标签删除掉,tableviewcell中的代码如下:

-(void)setTagArray:(NSArray *)tagArray{

_tagArray = tagArray;

NSArray *currentArray = tagArray;

NSInteger index = 0;

CGFloat width = 0;

CGFloat top = 15;

if (tagArray != nil) {

if (currentArray.count == 0) {

UIView *smallView = [UIView new];

[self.contentView addSubview:smallView];

[smallView mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.right.top.bottom.equalTo(self.contentView);

make.height.mas_equalTo(0.1);

}];

}

}

for (NSDictionary *tagDic in currentArray) {

index ++;

CGRect rect = [tagDic[@"name"] boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil];

UILabel *nameLabel = [[UILabel alloc]init];

nameLabel.text = SNHNoNilString(tagDic[@"name"]);

nameLabel.font = [UIFont systemFontOfSize:13];

nameLabel.textAlignment = NSTextAlignmentCenter;

nameLabel.layer.cornerRadius = 3;

nameLabel.layer.borderWidth = 0.5;

nameLabel.layer.borderColor = [UIColor snh_d5d6d7Color].CGColor;

[self.contentView addSubview:nameLabel];

if (index == 1) {

width = 15 + rect.size.width + 25 + 12;

}else{

width = width + rect.size.width + 25 + 12;

}

if ((width + 15) > snh_screenWidth()) {

top = top + 40;

}

if (_previousLabel == nil) {

[nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.mas_equalTo(self.contentView.mas_left).offset(15);

make.top.mas_equalTo(self.contentView.mas_top).offset(top);

if (index == _tagArray.count) {

make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-15);

}

make.size.mas_equalTo(CGSizeMake(rect.size.width + 25, 25));

}];

}else{

[nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {

NSLog(@"====%f",_previousLabel.frame.origin.x);

if ((width + 15) > snh_screenWidth()) {

make.left.mas_equalTo(self.contentView.mas_left).offset(15);

make.top.mas_equalTo(self.contentView.mas_top).offset(top);

if (index == _tagArray.count) {

make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-15);

}

}else{

make.left.mas_equalTo(_previousLabel.mas_right).offset(12);

make.centerY.mas_equalTo(_previousLabel.mas_centerY);

if (index == _tagArray.count) {

make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-15);

}

}

make.size.mas_equalTo(CGSizeMake(rect.size.width + 25, 25));

}];

}

if ((width + 15) > snh_screenWidth()) {

width = 15 + rect.size.width + 25 + 12;

}

_previousLabel = nameLabel;

}

}




以上是代码,要注意最后一个标签与cell底部有约束,来实现自动行高

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,392评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,834评论 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,195评论 1 4
  • 一、前言 关于苹果的布局一直是我比较纠结的问题,是写代码来控制布局,还是使用storyboard来控制布局呢?以前...
    iplaycodex阅读 2,476评论 0 1
  • 受难记 陈宸 周五放学前,老班宣布:“因为中考三天调休,接下来的三个周六,都要到校上课。”此话一出,顿时怨声载道,...
    苏北以南阅读 398评论 0 1