1.cell .h文件
// WorkReportCell.h
// GOSProject
//
// Created by on 2017/5/11.
// Copyright © 2017年 All rights reserved.
//
#import
@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.效果图片------如下图所示
