iOS---cell-自适应高度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS---cell-自适应高度相关的知识,希望对你有一定的参考价值。

RootViewController:

//
//  RootViewController.m
//  UI__cell自适应高度
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "MyTableViewCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>

@property (nonatomic, retain)NSArray *picArr;
@property (nonatomic, retain)NSArray *ziArr;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 200;
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.picArr = @[@"01.jpg", @"00.jpg", @"02.jpg"];
    self.ziArr = [NSMutableArray arrayWithObjects:@"中国新闻网北京4月1日电 (万鹏)3月28日,主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];
  
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.picImageView.image = [UIImage imageNamed:self.picArr[indexPath.row]];
    cell.newsLabel.text = self.ziArr[indexPath.row];
    return  cell;
 
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //根据图片的尺寸算出cell的高度
    UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
    CGFloat height = self.view.frame.size.width / image.size.width * image.size.height;
    
    //计算文字的高度
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];//常量字符串
    CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
   
    return height + rect.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.picArr.count;
    
}
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (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

MyTableViewCell:

//
//  MyTableViewCell.m
//  UI__cell自适应高度
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyTableViewCell.h"

@implementation MyTableViewCell
- (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.contentView addSubview:self.picImageView];
    [_picImageView release];
    
    self.newsLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.newsLabel];
    [_newsLabel release];
    
    self.newsLabel.font = [UIFont systemFontOfSize:14];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%g", self.picImageView.image.size.width);
    //算的是imageView的尺寸
    CGFloat h = self.contentView.frame.size.width / self.picImageView.image.size.width * self.picImageView.image.size.height;
    self.picImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, h);
    NSLog(@"%@", self.newsLabel.text);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];//常量字符串
    CGRect rect = [self.newsLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    self.newsLabel.frame = CGRectMake(0, h, self.viewForFirstBaselineLayout.frame.size.width, rect.size.height);
    self.newsLabel.numberOfLines = 0;
}



- (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---cell-自适应高度的主要内容,如果未能解决你的问题,请参考以下文章

HTML标签的TABLE高度怎么自适应

怎样让一个div高度自适应浏览器高度

Iframe的代码怎么写才能满足我的自适应内容的高度的要求啊?

vue下iframe怎么自适应高度

iframe高度自适应,终于解决了

jQuery解决iframe高度自适应代码