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(, , WIDTH, HEIGHT)];
self.tableView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.tableView];
self.tableView.rowHeight = ;
[_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:], NSFontAttributeName, nil];//常量字符串
CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(, ) 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:];
} - (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(, , self.contentView.frame.size.width, h);
NSLog(@"%@", self.newsLabel.text);
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:], NSFontAttributeName, nil];//常量字符串
CGRect rect = [self.newsLabel.text boundingRectWithSize:CGSizeMake(, ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
self.newsLabel.frame = CGRectMake(, h, self.viewForFirstBaselineLayout.frame.size.width, rect.size.height);
self.newsLabel.numberOfLines = ;
} - (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-自适应高度的更多相关文章

  1. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  2. TableView cell自适应高度-----xib

    1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...

  3. IOS UITextView自适应高度

    LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...

  4. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  5. cell自适应高度

    MyModel.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MyModel : ...

  6. 【原】ios tableViewCell 自适应高度

    原文:http://www.cnblogs.com/A--G/p/4819051.html 前言:之前在做一个类似微博的小需求时候,用table view实现了微博文字和图片等等的基本展示,由于文字和 ...

  7. 自定义 cell 自适应高度

    #import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...

  8. iOS Label 自适应高度

    推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...

  9. IOS XIB Cell自适应高度实现

    1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [ ...

  10. iOS之UITableView加载网络图片cell自适应高度

    #pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...

随机推荐

  1. 标准C IO函数和 内核IO函数 效率(时间)比较

    前言 标准C提供的文件相关的IO函数,除标准错误输出是不带缓冲的(可以尽快的将错误消息显示出来)之外,所有与终端相关的都是行缓冲,其余都是全缓冲的. 我们可以使用setbuf,setvbuf改变指定流 ...

  2. 迅为顶级四核开发板 Exynos4412开发板,仅售560元

    业内公认最强四核 藐视一切挑战 ●四核CPU  ●四核GPU  ●双通道内存  ●HKMG技术  ●超级性能  ●至佳稳定  ●供货周期长 Exynos 4412处理器成功应用于三星GALAXY S3 ...

  3. Linux shell basic2 cat find tr

    Cat stands for concatenate. Case 1. When the text files have more blank lines, we want to remove the ...

  4. EntityFramework6 快速入门教程

    EntityFramework6 快速入门教程 不得不说EF在国内实在是太小众,相关的技术文章真实屈指可数,而且很多文章都很旧了,里面使用的版本跟如今的EF6差别还是比较大.我刚开始弄这个的时候真是绕 ...

  5. MBean的学习

    参考:http://tuhaitao.iteye.com/blog/786391 这里以MBean对象进行演示向服务器注册,调用的过程. 1.MBean接口,接口名必须以MBean结尾 package ...

  6. zookeeper适用场景:配置文件同步

    问题导读:1.本文三个角色之间是什么关系?2.三个角色的作用是什么?3.如何代码实现这三个角色的作用? 在 zookeeper适用场景:zookeeper解决了哪些问题有关于分布式集群配置文件同步问题 ...

  7. 【Android UI设计与开发】3.引导界面(三)实现应用程序只启动一次引导界面

    大部分的引导界面基本上都是千篇一律的,只要熟练掌握了一个,基本上也就没什么好说的了,要想实现应用程序只启动一次引导界面这样的效果,只要使用SharedPreferences类,就会让程序变的非常简单, ...

  8. 如何在ZBrush中将球体演变为头发

    对于头发的制作,ZBrush®是多样的,前面的教程中也有讲解,可以用球体作为子工具,用不同笔刷进行雕刻:还可以使用Z球转网格的方式.今天小编讲述一种最简单的方法,使用球体演变头发,这样的创作手法更高效 ...

  9. 边工作边刷题:70天一遍leetcode: day 75-2

    Strobogrammatic Number I/II/III 要点:记题,注意轴对称和点对称的区别.这题就是几个固定digit之间的palindrome I https://repl.it/CqLu ...

  10. You are note Hk

    直接打开是forbidden  最后一句话提示 于是改包 第二次还是修改包 Mozilla/5.0 (MSIE 7.0; Windows NT 6.0;.NET CLR 9.9)