iOS cell高度自适应_动态调节cell高度
2015-11-20 18:08
232 查看
在开发中, 经常碰到cell需要动态调节的情况, 例如聊天页面等, 但是IOS不像Android一样有wrap(wrap就是根据内容的大小,自动调节), 所以在IOS端处理cell高度就蛋疼了
下面介绍三种方法动态调节
如果你的cell内容比较简单, 就一个label或者cell文字可能多行的label在cell最末端, 那么只需要计算这个label的size, 然后赋值给cell即可:
如果cell多个label可能多行, 那么就需要计算多个label的size, 最好在cell的setModel方法里面进行
在- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath重新设置下model即可
github:FDTemplateLayoutCell
如果使用时height返回不正确,请看/article/3686899.html
设置tableView的估算Cell高度&rowHeight值为自动计算模式
设置约束, 最下面的view要上下左右都设置约束,不然无法计算正确高度
注意: - 不能实现heightForRow代理方法!!!
下面介绍三种方法动态调节
方法1-手动计算
在设置cell的model的时候, 调节动态计算cell的高度, 然后再去设置cell的高度如果你的cell内容比较简单, 就一个label或者cell文字可能多行的label在cell最末端, 那么只需要计算这个label的size, 然后赋值给cell即可:
[code]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"Cell"; CGFloat padding = 20; CJDemoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"CJDemoTableViewCell" owner:self options:nil] lastObject]; } CJDemoModel *model = self.models[indexPath.row]; cell.model = model; //此处是个计算size的string分类,后面会附上 cell.lastLabel.size = [model.testTest sizeWithFont:[UIFont systemFontOfSize:17] maxW:contentLW]; cell.height = CGRectGetMaxY(cell.lastLabel.frame) + padding; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.height; }
如果cell多个label可能多行, 那么就需要计算多个label的size, 最好在cell的setModel方法里面进行
[code]//cell setModel方法 - (void)setModel:(CJDemoModel *)model{ _model = model; CGFloat padding = 5; self.nameL.text = model.name; CGSize nameSize = [self.nameL.text sizeWithFont:[UIFont systemFontOfSize:17]]; nameSize.frame = CGRectMake(0,0,nameSize.width,nameSize.heigth); self.scoreL.text = model.score; CGSize scoreSize = [self.scoreL.text sizeWithFont:[UIFont systemFontOfSize:17]]; scoreL.frame = CGRectMake(0,CGRectGetMaxY(self.nameL.frame),scoreSize.width,scoreSize.heigth); self.jobL.text = model.job; CGSize jobSize = [self.jobL.text sizeWithFont:[UIFont systemFontOfSize:17]]; self.jobL.frame = CGRectMake(0,CGRectGetMaxY(self.scoreL.frame),jobSize.width,jobSize.height); self.cellHeight = CGRectGetMaxY(self.jobL.frame) + padding; } //然后在设置cell的高度(controller) - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.cellHeight; }
方法2-FDTemplateLayoutCell
借助框架UITableView-FDTemplateLayoutCell在- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath重新设置下model即可
[code]- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return = [tableView fd_heightForCellWithIdentifier:ID configuration:^(CJDemoTableViewCell *cell) { CJDemoModel *model = self.models[indexPath.row]; cell.model = model; }]; } }
github:FDTemplateLayoutCell
如果使用时height返回不正确,请看/article/3686899.html
方法三-IOS8特性
IOS8中, 提供了动态调节cell高度的方法, 通过XIB添加约束即可, 如果你的应用要适配IOS7及以下, 那么可以参考前两种方法设置tableView的估算Cell高度&rowHeight值为自动计算模式
[code]- (void)viewDidLoad { [super viewDidLoad]; self.tableView.estimatedRowHeight = 100; // 随便设个不那么离谱的值 self.tableView.rowHeight = UITableViewAutomaticDimension; }
设置约束, 最下面的view要上下左右都设置约束,不然无法计算正确高度
注意: - 不能实现heightForRow代理方法!!!
本文用到的String_category
[code]- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW { NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = font; CGSize maxSize = CGSizeMake(maxW, MAXFLOAT); return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } - (CGSize)sizeWithFont:(UIFont *)font { return [self sizeWithFont:font maxW:MAXFLOAT]; }
相关文章推荐
- IOS block
- ios 本地推送
- IOS常用的设计模式以及对应的优势
- 使用.a文件遇到的错误
- 利用biostime()读取并设置BIOS的时钟
- iOS应用性能调优的25个建议和技巧
- iOS 利用平移缩放旋转手势对view实现对应的平移缩放旋转效果(一)
- IOS 截取部分图片并显示
- ios 调用系统的地图
- iOS上简单推送通知
- iOS 9 Auto Layout界面自动布局系列6-自适应布局
- iOS 之证书问题
- iOS PPI
- nagios监控
- ios h5 出现的问题
- iOS 标签字体加粗
- iOS 根证书和 P12 区分
- iOS开发 单手使触摸的视图旋转
- iOS- Masonry自动布局框架介绍与使用实践
- iOS 表视图的分区头部文字设置