UI0_cell自适应高度

//
//  MainViewController.m
//  UI0_cell自适应高度
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MainViewController.h"
#import "MyCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@property(nonatomic, retain)NSArray *picArr;
@property(nonatomic, retain)NSArray *ziArr;

@end

@implementation MainViewController

- (void)dealloc
{
    [_picArr release];
    [_ziArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg",nil];
        self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview: tableView];
    [tableView release];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    // 它只会执行一次,可以设置行高,但是不灵活
    // tableView.rowHeight = 300;
    
}
#pragma mark 这个方法是tableview的delegate所提供的协议方法,主要是用来设置每一行高度的
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据图片的尺寸设置行高
    //
    UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
    // 通过CGSize找到image里面的图片尺寸
    CGSize picSize = image.size;
    // 计算行高
    CGFloat rowHeight = picSize.height * self.view.frame.size.width / picSize.width;
    
    // 计算label的高度
    // 字号要统一
    // 根据对应的文字求出cell上label显示的高度
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    
    // 根据文字的大小,计算出文本的尺寸
    // 还需要指定一个尺寸(375, 0),0是范围的最大值
    // 第三个参数:计算高度需要依据字体的哪个特征来确定
    CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    // 最后把结果作为返回值返回
    return rowHeight + rect.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.picArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    cell.picImageView.image = [UIImage imageNamed:self.picArr[indexPath.row]];
    
    cell.myLabel.text = self.ziArr[indexPath.row];
    return  cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

//
//  MyCell.h
//  UI0_cell自适应高度
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import 

@interface MyCell : UITableViewCell
// 自定义属性的时候不能和系统名重名
@property(nonatomic, retain)UIImageView *picImageView;
@property(nonatomic, retain)UILabel *myLabel;
@end

//
//  MyCell.m
//  UI0_cell自适应高度
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MyCell.h"

@implementation MyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.picImageView = [[UIImageView alloc] init];
    self.picImageView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.picImageView];
    [_picImageView release];
    
    self.myLabel = [[UILabel alloc] init];
    // 指定label的字体大小,字体的大小默认是17号
    self.myLabel.font = [UIFont systemFontOfSize:14];
    // 0是最大行数
    self.myLabel.numberOfLines = 0;
    [self.myLabel sizeToFit];
    
    [self.contentView addSubview:self.myLabel];
    [_myLabel release];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 让imageview的尺寸和cell的图片大小相同
    
    // 因为这个方法是最后一个被执行的,所以执行到这个方法的时候,已经对cell的各个属性进行完赋值操作,所以可以通过imageview.image找到图片尺寸
    CGSize picSize = self.picImageView.image.size;
    CGFloat height = self.contentView.frame.size.width * picSize.height / picSize.width;
    self.picImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height);
    
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    
    // 根据文字的大小,计算出文本的尺寸
    // 还需要指定一个尺寸(375, 0),0是范围的最大值
    // 第三个参数:计算高度需要依据字体的哪个特征来确定
    CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    
    self.myLabel.frame = CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height);
}
- (void)dealloc
{
    [_picImageView release];
    [_myLabel release];
    [super dealloc];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


你可能感兴趣的:(iOS,UI,iOS前期训练,UI)