tableViewCell自适应高度

#import "RootViewController.h"
#import "MyCell.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)NSMutableArray *ziArr;
@end

@implementation RootViewController

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor cyanColor];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView.rowHeight = 80;
}
**cell自适应高度**
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据图片的不同尺寸,返回不同的高度
    // 1.先根据图片名找图片UIImage
    UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
    // 根据图片原有尺寸计算比例,然后根据目标宽度,计算出返回高度
    CGFloat picHeight = self.view.frame.size.width * image.size.height / image.size.width;

    // 要注意字体大小保持一致
    // 文字的大小作为一个描述信息放到字典中成为一对key-value
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数:文字显示的最大尺寸范围(375, 0) 0是代表最大的意思
    CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return picHeight + 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:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.firstImageView.image = [UIImage imageNamed:self.picArr[indexPath.row]];
    cell.textLabel.text = @"1";
    cell.myLabel.text = self.ziArr[indexPath.row];
    cell.detailTextLabel.text = @"12";
    return  cell;
}

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


@end
#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property(nonatomic, retain)UIImageView *firstImageView;
@property(nonatomic, retain)UILabel *myLabel;

@end
#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.firstImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.firstImageView];
    self.firstImageView.layer.borderWidth = 1;
    self.firstImageView.layer.cornerRadius = 10;
    [_firstImageView release];

    self.myLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.myLabel];
    [_myLabel release];
    // 设置myLabel的行数
    self.myLabel.numberOfLines = 0;
    // 设置字体和大小
    self.myLabel.font = [UIFont systemFontOfSize:16];




}
- (void)layoutSubviews
{
    [super layoutSubviews];
    **自定义cell高度**
    // 最后走的一个方法,在这个方法里,属性已经被赋好值了
    // 也是根据图片的尺寸,计算高度
    CGFloat picHeight = self.contentView.frame.size.width * self.firstImageView.image.size.height / self.firstImageView.image.size.width;
    self.firstImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, picHeight);

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数:文字显示的最大尺寸范围(375, 0) 0是代表最大的意思
    CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 设置myLabel的尺寸
    self.myLabel.frame = CGRectMake(0, picHeight, self.contentView.frame.size.width, rect.size.height);
}

- (void)awakeFromNib {
    // Initialization code
}


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

    // Configure the view for the selected state
}

@end

猜你喜欢

转载自blog.csdn.net/s_huayangnianhua/article/details/50231837