cell的自适应高度
来源:互联网 发布:掉头发 白头发 知乎 编辑:程序博客网 时间:2024/05/04 19:26
设有三张图及三张段文字并且一一对应匹配,那么应该获取显示文本Label的尺寸和相应图片的UIImageView尺寸
需要注意的是无论图片的大小如何变化,但是图片的长宽之比是不变的
(1).求cell高度的方法是tableview的delegate所提供的协议方法,主要是用来设置每一行高度该方法在MainViewController.m写
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
只有三张图片所以indexPath范围是0 - 2;
根据图片的尺寸设置行高
UIImage *image = [UIImageimageNamed:self.picArr[indexPath.row]];
通过CGSize找到image里面图片的尺寸
CGSize picSize = image.size;
计算行高
CGFloat rowHeight = picSize.height *WIDTH / picSize.width;
图片的高度和文字的高度加起来是cell的高度
根据对应的文字求出cell上label显示的高度
NSDictionary *fontDic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:14]; NSFontAttributeName,nil];
根据文字的大小,计算出文本的尺寸
还需要指定一个尺寸(375, 0)( 0是最大值)
第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(WIDTH,0) options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil];
最后把结果作为返回值返回
return rowHeight + rect.size.height;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[selfcreateView];
}
returnself;
}
-(void)createView
{
self.image1 = [[UIImageViewalloc]init];
self.image1.backgroundColor = [UIColorcyanColor];
[self.contentViewaddSubview:self.image1];
[self.image1release];
self.myLabel = [[UILabelalloc]init];
self.myLabel.backgroundColor = [UIColorgrayColor];
指定label的字体大小
self.myLabel.font = [UIFontsystemFontOfSize:14];
0是最大行数
self.myLabel.numberOfLines =0;
[self.myLabelsizeToFit];
[self.contentViewaddSubview:self.myLabel];
[self.myLabelrelease];
}
-(void)layoutSubviews
{
[superlayoutSubviews];
让imageView的尺寸和cell的图片大小相同
因为这个方法是在uihou一个被执行的,所以执行到这个方法的时候已经对cell的各个属性进行完整赋值操作,所以可以通过imageview,image找到图片的尺寸
CGSize picSize =self.image1.image.size;
CGFloat height =self.contentView.frame.size.width * picSize.height / picSize.width;
self.image1.frame =CGRectMake(0,0, self.contentView.frame.size.width, height)
NSDictionary *fontDic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:14],NSFontAttributeName, nil];
根据文字的大小,计算出文本的尺寸
还需要指定一个尺寸(375, 0)// 0是最大值
第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect = [self.myLabel.textboundingRectWithSize:CGSizeMake(375,0) options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil];
self.myLabel.frame =CGRectMake(0, height,self.contentView.frame.size.width, rect.size.height);
}
-(void)dealloc
{
[self.image1release];
[superdealloc];
}
使用方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *reuse = @"reuse";
zidingyiTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuse];
if (cell ==nil) {
cell = [[[zidingyiTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse] autorelease];
}
cell.image1.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%ld.png", indexPath.row +1]];
cell.myLabel.text =self.ziArr[indexPath.row];
return cell;
}
- cell的自适应高度
- tableView 的cell自适应高度
- UITableView的cell自适应高度
- UITableView的cell自适应高度
- Swift cell的自适应高度
- Cell 的简单自适应高度
- cell的高度自适应cell里lable的高度
- iOS 8cell的高度的自适应
- 表的cell的高度自适应
- Tableview中cell的自适应高度
- tableView自适应cell的高度,提高性能
- iOS8 tableView的Cell高度自适应开发
- 使用storyboard tableView的cell自适应高度
- iOS开发中cell高度的自适应
- [iOS]TableView的Cell高度自适应
- UItableView cell 自适应高度
- iOS Cell高度自适应
- ios cell自适应高度
- LeetCode解题报告--Roman to Integer
- 代码手写UI,xib和StoryBoard间的博弈
- 【汇总】资源下载网址
- 深入JavaScript(15)JavaScript核心(晋级高手必读篇)
- HDU 5389 Zero Escape
- cell的自适应高度
- 用@property声明的NSString(或NSArray,NSDictionary)经常使用copy关键字,为什么?如果改用strong关键字,可能造成什么问题?
- 求指导pat的1003题,改了二三十遍都同通不过,求各位看看到底错哪了
- 超级简单的string 类型转换成char*类型的方法。
- QTranslator的使用
- hdu 5389 Zero Escape 2015多校联合训练赛#8 动态规划
- 贝叶斯公式,全概率公式
- ExecuteInEditMode加在类前面即是没有运行也能产生效果
- iOS百度地图SDK常见问题以及解决方案