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. R语言中数据框的横向合并与纵向合并

    #横向合并ID<-c(1,2,3,4)name<-c("Jim","Tony","Lisa","Tom")s ...

  2. xamarin.android 图片高斯模糊效果

    代码如下: private static float BITMAP_SCALE = 0.1f; private static float BLUR_RADIUS = 12.0f; public sta ...

  3. Hadoop step by step _ install and configuration environment

    1.安装centos linux系统. 2.配置静态IP 3.配置防火墙 4.添加hadoop用户 5.检查并安装jdk 配置环境变量 6.配置sshd服务 7.配置ssh免密码登录 8.格式化nam ...

  4. appium 常用api介绍(2)

    前言:接着上一篇继续讲常用的一些api 参考博文:http://blog.csdn.net/bear_w/article/details/50330565 1.send_keys send_keys( ...

  5. leetcode_438_Find All Anagrams in a String_哈希表_java实现

    题目: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Stri ...

  6. 怎么通过js获取上传的图片信息(临时保存路径,名称,大小)然后通过ajax传递给后端?

    今天在论坛上看到这样一个问题,有必要编辑搜集下. 问题描述:怎么通过js获取上传的图片信息(临时保存路径,名称,大小)然后通过ajax传递给后端 题主用jquery接收 <input name= ...

  7. HDU 5029 Relief grain --树链剖分第一题

    题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...

  8. HDU 1394 & ZOJ 1484 Minimum Inversion Number

    (更新点查询区间) 这题重在想到,写代码很容易了..这题是利用线段树求逆序数,不按给定的顺序建树,而是有序地插入.比如每插入一个数,就统计之前插入的那些数里比他大的有多少个,这个数就是此时的逆序数,然 ...

  9. Java语法基础(二)----运算符

    一.运算符: 运算符包括下面几种: 算术运算符 赋值运算符 比较运算符 逻辑运算符 位运算符 三目运算符 最不常用的是位运算符,但也是最接近计算机底层的. 1.算术运算符 (1)+的几种用法:加法.正 ...

  10. 运维工作中常用到的几个rsync同步命令

    作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync. 下面结合本人近几年运维工作中对这一 ...