cell自适应高度
来源:互联网 发布:侠客风云传官网优化 编辑:程序博客网 时间:2024/05/20 23:37
cell自适应高度
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{
}
- UItableView cell 自适应高度
- iOS Cell高度自适应
- ios cell自适应高度
- iOS8 Cell 高度自适应
- Cell自适应高度
- cell单元格高度自适应
- cell自适应高度
- cell自适应高度
- cell的自适应高度
- cell自适应高度
- Cell自适应高度
- cell高度自适应
- cell高度自适应
- cell自适应高度
- cell自适应高度
- cell自适应高度
- masonry + 自适应cell高度
- cell自适应高度
- 修改已存在文件
- SQL多表update
- jdk自带的jvm监控工具
- 26-网络编程-01-网络编程(网络模型概述)
- 返回上一层activity
- cell自适应高度
- 内存管理(一)
- 如何通过MyEclipse生成含有第三方包的jar包
- 函数在线模拟
- SSH基本应用命令
- 块设备驱动学习过程
- 上市公司行情查询网站
- Android SERVICE后台服务进程的守护
- 利用Navicat传输MySql中不同数据库的数据