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. 初探Team Foundation Server (TFS) 2015 REST API

    REST是一种简洁方便的Web服务,通过基于http协议的远程通信,可以为多种客户端程序提供远程服务,大幅提高了服务器系统的可扩展性. 微软宣布从Team Foundation Server 从201 ...

  2. GoLang 的 daemonize 实现

    func daemonize(cmd string, args []string, pipe io.WriteCloser) error { pid, _, sysErr := syscall.Raw ...

  3. 传统高斯模糊与优化算法(附完整C++代码)

    高斯模糊(英语:Gaussian Blur),也叫高斯平滑,是在Adobe Photoshop.GIMP以及Paint.NET等图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次 ...

  4. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

  5. a 标签中调用js的几种方法

    我们常用的在a标签中有点击事件: 1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问 ...

  6. mysql创建表

    说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 在sql语句中注意“约束的概念": 1.实体完整性约束(主键--唯一且非空) primary key() 违约处理:No a ...

  7. 第三方登录 ----转载自简书,作者 <<碧霄问鼎>>

    这几天遇到一个需求:做第三方登录和分享.遇到了一些坑,把整个过程整理记录下来,方便他人,同时也捋一下思路. 当时考虑过把每个平台的SDK下载下来,一个一个弄,一番取舍后决定还是用ShareSDK.这里 ...

  8. 过滤掉combobox里名称相同的选项

    var pname = ""; $('#PartName').combobox({ reload: url, formatter: function (row) {//过滤comb ...

  9. BPM到底能做什么?K2为你解读

    和平镇,镇如其名,几百年来一直很和平,夜不闭户路不拾遗.可是这一年来,镇上金光寺的和尚却开始不断离奇死亡…… 衙门里新调来的李捕头正好负责这个案子,经过了几个月的不眠不休,现场侦查和缜密推理之后,一切 ...

  10. CentOS 7设置网络开机自动连接

    用root登陆系统 修改/etc/sysconfig/network-scripts/ifcfg-enpxxxxxx(xxx)文件,其内容原本如下 TYPE=Ethernet BOOTPROTO=dh ...