1. 程式人生 > >UItableView Cell 高度自適應--隨著多個label內容變化自適應

UItableView Cell 高度自適應--隨著多個label內容變化自適應

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:@"
[email protected]
"]; 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.效果圖片------如下圖所示