cell自适应高度
2015-08-11 17:12
232 查看
cell自适应高度
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createData];
}
return self;
}
-(void)createData{
self.picImageView=[[UIImageView alloc] init];
[self.contentView addSubview:self.picImageView];
// self.oneImageView.backgroundColor=[UIColor greenColor];
[self.picImageView release];
// self.myLabel.textAlignment=NSTextAlignmentCenter;
}
-(void)layoutSubviews{
[super layoutSubviews];
// 让imageView的尺寸和cell的图片大小相同
// 因为这个方法是最后一个被执行的,所以执行到这个方法的时候,应经对cell的各个属性进行完赋值操作,所以可以通过imageView.image找到图片的尺寸
CGSize picSize=self.picImageView.image.size;
CGFloat height=WIDTH*picSize.height/picSize.width;
self.picImageView.frame=CGRectMake(0, 0, WIDTH, height);
}
(void)dealloc
{
[_myLabel release];
[_picImageView release];
[super dealloc];
}
// 根据图片的尺寸,设置行高
UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]];
// 通过CGSize找到image里面图片的尺寸
CGSize picSize=image.size;
// 计算行高
CGFloat rowHeight=picSize.height*WIDTH/picSize.width;
// 计算label的高度
// 根据对应的文字求出cell上label显示的高度
NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
// 根据文字的大小,计算出文本的尺寸
// 还需要制定一个尺寸(375,0)0是高度最高的
// 第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect=[self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
// 最后把结果作为返回结果
return rowHeight+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 dequeueReusableHeaderFooterViewWithIdentifier:reuse];
if (!cell) {
cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
}
cell.picImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];
cell.myLabel.text=self.ziArr[indexPath.row];
return cell;
}
(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
}
import “MyCell.h”
define WIDTH self.contentView.frame.size.width
define HEIGHT self.contentView.frame.size.height
@implementation MyCell-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createData];
}
return self;
}
-(void)createData{
self.picImageView=[[UIImageView alloc] init];
[self.contentView addSubview:self.picImageView];
// self.oneImageView.backgroundColor=[UIColor greenColor];
[self.picImageView release];
self.myLabel=[[UILabel alloc] init]; [self.contentView addSubview:self.myLabel]; self.myLabel.textColor=[UIColor redColor];
// self.myLabel.textAlignment=NSTextAlignmentCenter;
// 指定label的字体大小,默认字体是17号 self.myLabel.font=[UIFont systemFontOfSize:14]; self.myLabel.numberOfLines=0; [self.myLabel sizeToFit]; [self.myLabel release];
}
-(void)layoutSubviews{
[super layoutSubviews];
// 让imageView的尺寸和cell的图片大小相同
// 因为这个方法是最后一个被执行的,所以执行到这个方法的时候,应经对cell的各个属性进行完赋值操作,所以可以通过imageView.image找到图片的尺寸
CGSize picSize=self.picImageView.image.size;
CGFloat height=WIDTH*picSize.height/picSize.width;
self.picImageView.frame=CGRectMake(0, 0, WIDTH, height);
NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]; CGRect rect=[self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; self.myLabel.frame=CGRectMake(0, height, WIDTH, rect.size.height);
}
(void)dealloc
{
[_myLabel release];
[_picImageView release];
[super dealloc];
}
import “MainViewController.h”
import “MyCell.h”
define WIDTH self.view.frame.size.width
define HEIGHT self.view.frame.size.height
@interface MainViewController ()pragma mark 这个方法是tableView的delegate所提供的协议方法,主要是用来设置每行高度
(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{// 根据图片的尺寸,设置行高
UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]];
// 通过CGSize找到image里面图片的尺寸
CGSize picSize=image.size;
// 计算行高
CGFloat rowHeight=picSize.height*WIDTH/picSize.width;
// 计算label的高度
// 根据对应的文字求出cell上label显示的高度
NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
// 根据文字的大小,计算出文本的尺寸
// 还需要制定一个尺寸(375,0)0是高度最高的
// 第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect=[self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
// 最后把结果作为返回结果
return rowHeight+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 dequeueReusableHeaderFooterViewWithIdentifier:reuse];
if (!cell) {
cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
}
cell.picImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];
cell.myLabel.text=self.ziArr[indexPath.row];
return cell;
}
(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
}
相关文章推荐
- boundingRectWithSize:options:attributes:context:计算尺寸的使用
- Cell 高度自适应 SDAutoLayout-Cell
- iOS cell高度自适应 - 教你写出优雅的table view
- TableView 展开和折叠
- iOS UITableView cell自适应内容高度
- cell的高度计算
- TableviewCell自适应高度之加载不同数量的图片。
- iOS 之cell自适应高度
- tableViewCell高度计算,自动计算 or 手动计算
- iOS雨笙 lable自适应高度(多用于tableview的自定义cell中)
- UITableView自动计算cell高度
- tableview和cell高度自适应
- iOS autolayout自适应cell高度时使用estimatedRowHeight的一些问题
- 内存管理(一)
- 返回上一层activity
- 如何通过MyEclipse生成含有第三方包的jar包
- 块设备驱动学习过程
- SSH基本应用命令
- 26-网络编程-02-网络编程(网络要素-IP地址)
- springMVC 【@response 返回对象自动变成json并且防止乱码】 & 【配置支持实体类中的@DateTimeFormat注解】