tableview非等高的cell
2016-06-03 10:54
169 查看
// - 方法1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QGMyCell * cell = [QGMyCell cellWithTableView:tableView];
cell.model = _dataArr[indexPath.row];
// - 强制 cell 重新布局 这样 在 cell 中计算的高度和 cell 的空间的位置才可以实时刷新,否则 cell 会直接调用 heightForRowAtIndexPath 方法 这时cell 中的布局还没有完成 model 中的高度属性是没有值的
[cell layoutIfNeeded];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heightForRowAtIndexPath -----%ld", (long)indexPath.row);
return 250;
}
// - 估计 cell 的高度, 在没写这个方法时候, tableView会直接调用 heightForRowAtIndexPath 然后在调用 cellForRowAtIndexPath 而且会一次把所有的高度都计算出来这样会出现卡对现象 为了防止这种情况 就调用一下 estimatedHeightForRowAtIndexPath 方法 这样 就会 先调用cellForRowAtIndexPath 在调用heightForRowAtIndexPath 而且是 调用一次 cellForRowAtIndexPath
然后调用一次 heightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
/** 因为如果上边的方法 那么 Controller 中的代码的冗余度太高了 没用的代码太对了 为了防止这种情况 就 有了方法的代码(就是给 modle 的属性多加了个 rowHeight 然后在 cell 中 计算完控件的坐标 给 modle 的 cellHeight 赋值之前 执行一步 [self layoutIfNeeded];) */
// - 方法2
#import "QGMyCell.h"
// - cell 从 xib 中布局获取到的时候 因为 xib 的自动布局问题 设置 _label.preferredMaxLayoutWidth = 320; 就是设置_ label 的布局的最大宽度是 320
- (void)awakeFromNib {
[super awakeFromNib];
_label.preferredMaxLayo
4000
utWidth = 320;
// Initialization code
}
-(void)setUp{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 250)];
_label.backgroundColor = [UIColor greenColor];
_label.lineBreakMode = NSLineBreakByCharWrapping;
_label.numberOfLines = 0;
[self.contentView addSubview:_label];
}
-(void)setModel:(QGMyModel *)model{
_label.text = model.title;
NSDictionary * attr = @{ NSFontAttributeName : [UIFont systemFontOfSize:12]};
CGSize size = [model.title boundingRectWithSize:CGSizeMake(320, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
CGRect rect = _label.frame;
rect.size.height = size.height;
[self layoutIfNeeded];
model.rowHeight = CGRectGetMaxY(_label.frame) + 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QGMyCell * cell = [QGMyCell cellWithTableView:tableView];
cell.model = _dataArr[indexPath.row];
// - 强制 cell 重新布局 这样 在 cell 中计算的高度和 cell 的空间的位置才可以实时刷新,否则 cell 会直接调用 heightForRowAtIndexPath 方法 这时cell 中的布局还没有完成 model 中的高度属性是没有值的
[cell layoutIfNeeded];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heightForRowAtIndexPath -----%ld", (long)indexPath.row);
return 250;
}
// - 估计 cell 的高度, 在没写这个方法时候, tableView会直接调用 heightForRowAtIndexPath 然后在调用 cellForRowAtIndexPath 而且会一次把所有的高度都计算出来这样会出现卡对现象 为了防止这种情况 就调用一下 estimatedHeightForRowAtIndexPath 方法 这样 就会 先调用cellForRowAtIndexPath 在调用heightForRowAtIndexPath 而且是 调用一次 cellForRowAtIndexPath
然后调用一次 heightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
/** 因为如果上边的方法 那么 Controller 中的代码的冗余度太高了 没用的代码太对了 为了防止这种情况 就 有了方法的代码(就是给 modle 的属性多加了个 rowHeight 然后在 cell 中 计算完控件的坐标 给 modle 的 cellHeight 赋值之前 执行一步 [self layoutIfNeeded];) */
// - 方法2
#import "QGMyCell.h"
// - cell 从 xib 中布局获取到的时候 因为 xib 的自动布局问题 设置 _label.preferredMaxLayoutWidth = 320; 就是设置_ label 的布局的最大宽度是 320
- (void)awakeFromNib {
[super awakeFromNib];
_label.preferredMaxLayo
4000
utWidth = 320;
// Initialization code
}
-(void)setUp{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 250)];
_label.backgroundColor = [UIColor greenColor];
_label.lineBreakMode = NSLineBreakByCharWrapping;
_label.numberOfLines = 0;
[self.contentView addSubview:_label];
}
-(void)setModel:(QGMyModel *)model{
_label.text = model.title;
NSDictionary * attr = @{ NSFontAttributeName : [UIFont systemFontOfSize:12]};
CGSize size = [model.title boundingRectWithSize:CGSizeMake(320, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
CGRect rect = _label.frame;
rect.size.height = size.height;
[self layoutIfNeeded];
model.rowHeight = CGRectGetMaxY(_label.frame) + 10;
}
相关文章推荐
- XMLHttp java服务端与 js客户端通讯实现二进制流收发
- Matlab中边缘提取方法简析
- 简单的图片轮播 加小圆点
- poj 3524 Charm Bracelet(01背包)
- KeilC51使用详解 (三)
- 课堂作业买书
- SDUT 3262 Colorful Cupcakes (2015年山东省第六届ACM大学生程序设计竞赛)
- file 上传控件的页面一定不能用UpdatePanel
- thinkphp查询一周内的新闻排行10条
- 电信数据挖掘中ID过少问题解决总结
- Java web的读取Excel简单实例代码
- zigbee基础应用(五)uart串口通信
- 关于core文件的生成
- reset
- Unity 对象查找
- Team Foundation Server 2015使用教程【2】:默认团队成员添加
- String、StringBuilder、StringBuffer对比
- 文章标题
- 品类父子品类枚举
- spring schedule定时任务(二):配置文件的方式