32.CELL⾃适应⾼度
2015-11-24 21:53
246 查看
之前我们使⽤的cell⼀直都是同样的⾼度,或者某⾏固定⾼度。
实际开发中经常要让cell根据Model中⽂本的⻓短动态的更改⾼度
方法1.取出被点击的Cell
MyTableViewCell cell = (MyTableViewCell )[tableView cellForRowAtIndexPath:indexPath];
步骤1.获取字符串的高度
步骤2. 改变一下label的高度
步骤3.改变每行的高度
在自定义cell.m文件中
重写了setter方法
- (void)setModel:(NewsModel *)model
{
if (_model != model) {
[_model release];
_model = [model retain];
}
self.titleLabel.text = model.title;
self.summaryLabel.text = model.summary;
利用model中的点选状态 解决cell复用的问题
需要每次被复用的cell 再进行一个与状态对应的赋值
if (model.isSelect == YES) {
self.imageV.image = [UIImage imageNamed:@”select”];
}else{
self.imageV.image = [UIImage imageNamed:@”cancel”];
}
步骤1.获取字符串的高度
CGFloat summaryHeight = [MyTableViewCell cellHeightForModel:model];
步骤2. 改变一下label的高度
self.summaryLabel.height = summaryHeight;
}
计算字符串的高度(用类方法方便外面调取)
+ (CGFloat)cellHeightForModel:(NewsModel *)model
{
创建字体大小的字典
NSDictionary *fontDic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
计算字符串高度
CGRect textRect = [model.summary boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
return textRect.size.height;
}
在根视图中
设置每行有多高
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
取出对应的model
NewsModel *model = self.dataArray[indexPath.row];
计算高度
CGFloat summary = [MyTableViewCell cellHeightForModel:model];
每行高度 上边距 + toplabel + 中间距离 + 动态label高度 + 下边距
步骤3.改变每行的高度
return summary + 20 + 40 + 20 + 20;
}
相关文章推荐
- android112 c代码打印日志,c反编译调用java
- Mockito实现原理探析 -- Mockito.when(...).thenReturn(...)的一个简化实现
- 使用requirejs加载多个插件
- 分配病房(C程序设计进阶第6周)
- Android任务和返回栈完全解析,细数那些你所不知道的细节
- NSnotification通知
- SpringMVC介绍之Validation
- eclipse连接oracle11g教程(win7系统)
- python基础_Scrapy爬虫基础学习一
- java中堆栈和队列的实现方式
- 响应式的前端框架bootstrap
- PAT1097 Deduplication on a Linked List
- Leetcode168: House Robber II
- Runtime
- iOS视图生命周期
- LBS地理位置距离计算方法之geohash算法
- application对象
- linux文件按最新时间排序
- 字符串处理
- 一致性协议