UItableView Cell 高度自适应--随着多个label内容变化自适应
来源:互联网 发布:v字仇杀队v是谁知乎 编辑:程序博客网 时间:2024/06/02 06:31
1.cell .h文件
// WorkReportCell.h// GOSProject//// Created by on 2017/5/11.// Copyright © 2017年 All rights reserved.//#import <UIKit/UIKit.h>@interface WorkReportCell : UITableViewCell@property (nonatomic, strong) UIImageView * imageView1;@property (nonatomic, strong) UILabel * nameLabel;@property (nonatomic, strong) UILabel * timeLabel1;@property (nonatomic, strong) UILabel * rightTimeLabel;@property (nonatomic, strong) UILabel * todayLabel;//上面label@property (nonatomic, strong) UILabel * tomorrowLabel;//中间label@property (nonatomic, strong) UILabel * suggestLabel;//下面的label@property (nonatomic, strong) UIView * lineView;@property (nonatomic, strong) UIView * bottomLineView;//多个label的自动换行-(void)setIntroductionText:(NSString*)text dayLabel:(NSString*)text1 suggestLabel:(NSString*)text2;@end
2.cell.m文件
#import "WorkReportCell.h"@implementation WorkReportCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self initUIControl]; } return self;}-(void)initUIControl{ _imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)]; //_imageView1.backgroundColor = [UIColor grayColor]; [self.contentView addSubview:_imageView1]; _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 + 5, 5, 80, 20)]; _nameLabel.font = [UIFont systemFontOfSize:15]; _nameLabel.textColor = [UIColor redColor]; //_titleLabel.backgroundColor = [UIColor yellowColor]; [self.contentView addSubview:_nameLabel]; _timeLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50 +5, 25, 80, 20)]; _timeLabel1.font = [UIFont systemFontOfSize:12]; _timeLabel1.textColor = [UIColor lightGrayColor]; //_timeLabel1.backgroundColor = [UIColor magentaColor]; [self.contentView addSubview:_timeLabel1]; _rightTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(55+80, 10, VIEW_W-135-10, 30)]; _rightTimeLabel.font = [UIFont systemFontOfSize:14]; _rightTimeLabel.textAlignment = NSTextAlignmentRight; //_timeLabel.backgroundColor = [UIColor magentaColor]; [self.contentView addSubview:_rightTimeLabel]; //分隔线 _lineView = [[UIView alloc] initWithFrame:CGRectMake(12, 50+1, VIEW_W, 0.3)]; _lineView.backgroundColor = [UIColor colorFromHexString:@"999999"]; [self.contentView addSubview:_lineView]; _todayLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 51+5, VIEW_W-20, 30)]; _todayLabel.backgroundColor = [UIColor magentaColor]; [self.contentView addSubview:_todayLabel]; _tomorrowLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.todayLabel.frame)+5, VIEW_W-20, 30)]; _tomorrowLabel.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:_tomorrowLabel]; _suggestLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.tomorrowLabel.frame)+2, VIEW_W-20, 30)]; _suggestLabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:_suggestLabel]; _bottomLineView = [[UIView alloc] init]; _bottomLineView.backgroundColor = [UIColor lightGrayColor]; [self.contentView addSubview:_bottomLineView]; }//赋值 and 自动换行,计算出cell的高度-(void)setIntroductionText:(NSString*)text dayLabel:(NSString*)text1 suggestLabel:(NSString*)text2{ //获得当前cell高度 CGRect frame = [self frame]; //文本赋值 self.todayLabel.text = text; //设置第一个label的最大行数 self.todayLabel.numberOfLines = 10; CGSize size = CGSizeMake((VIEW_W-20), 1000); CGSize labelSize = [self sizeWithText:self.todayLabel.text font:self.todayLabel.font maxSize:size]; self.todayLabel.frame = CGRectMake(self.todayLabel.frame.origin.x, self.todayLabel.frame.origin.y, labelSize.width, labelSize.height); //文本赋值 self.tomorrowLabel.text = text1; //设置第二个label的最大行数 self.tomorrowLabel.numberOfLines = 10; CGSize size1 = CGSizeMake((VIEW_W-20), 1000); CGSize labelSize1 = [self sizeWithText:self.tomorrowLabel.text font:self.tomorrowLabel.font maxSize:size1]; self.tomorrowLabel.frame = CGRectMake(self.tomorrowLabel.frame.origin.x, CGRectGetMaxY(self.todayLabel.frame)+5, labelSize1.width, labelSize1.height); //文本赋值 self.suggestLabel.text = text2; //设置第三个label的最大行数 self.suggestLabel.numberOfLines = 10; CGSize size2 = CGSizeMake((VIEW_W-20), 1000); CGSize labelSize2 = [self sizeWithText:self.suggestLabel.text font:self.suggestLabel.font maxSize:size2]; self.suggestLabel.frame = CGRectMake(self.suggestLabel.frame.origin.x, CGRectGetMaxY(self.tomorrowLabel.frame)+5, labelSize2.width, labelSize2.height); //计算出自适应的高度 上面 头像和名字 + 三个label 高度 frame.size.height = labelSize.height+ 62 + 9 + labelSize1.height + labelSize2.height; self.frame = frame;}- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;}@end
3.控制器中的设置----高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ WorkReportCell * cell = (WorkReportCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height;}
4.控制器cell显示-------数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ WorkReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[WorkReportCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.imageView1.image = [UIImage imageNamed:@"lianxi@2x"]; cell.nameLabel.text = @"哈哈"; cell.timeLabel1.text = @"2017-5"; cell.rightTimeLabel.text = @"2017-5-12"; //上下三个lablel要现实的数据 [cell setIntroductionText:@"今日工作内容:就是完善各个界面和一些细节功能!除了上班晚上要LOL两把,然后哈哈哈哈哈哈你懂得" dayLabel:@"明日工作计划:除了上班晚上要LOL两把,然后哈哈哈哈哈哈你懂得!" suggestLabel:@"问题及建议:工作是生活的一部分,不要让工作成为生活的负担"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头 return cell;}
5.效果图片------如下图所示
阅读全文
1 0
- UItableView Cell 高度自适应--随着多个label内容变化自适应
- UItableView cell 自适应高度
- label宽度随着内容自适应
- iPhone UItableView cell 自适应高度
- swift UITableView cell自适应高度
- swift UITableView cell自适应高度
- UITableView的cell自适应高度
- UITableView的cell自适应高度
- StoryBoard设置Cell中的Label自适应高度
- swift 中cell 自适应label高度
- swift 中cell 自适应label高度
- UIday1103:UITableView 3 cell高度自适应代码
- UITableView中自定义Cell,自适应高度
- 【iOS开发】UITableView Cell 自适应高度
- 【自用】UITableView每个cell高度自适应
- IOS UItableview UIcollectionview cell高度自适应
- ios 中内容label高度自适应
- Swift3.0 label内容高度自适应
- 阿里云linux上安装docker
- 想知道需求分析实战课程会讲什么内容吗?
- hadoop 初识,环境配置,运行demo
- ios中的单例模式
- 提交到远程库
- UItableView Cell 高度自适应--随着多个label内容变化自适应
- Python 基本语法
- 面向对象的程序设计——理解对象
- h
- Oracle 常用 SQL 语句大全
- 使用百度Ueditor编辑器的时候,如何才能获取图片的保存路径,即src的值,以便满足需求:将路径存入数据库
- quartz spring 实现动态定时任务
- android 如何在service中定时请求网络
- 删除日志文件+日志文件组/日志文件状态+删除日志文件组