65.TableViewCell 自适应高度最优方法
来源:互联网 发布:金山恢复软件大师 编辑:程序博客网 时间:2024/05/30 23:15
在开发中遇到自适应高度的 tableviewcell 是很常见的, 但是到底这些方法中那些才是最优的呢, 今天就来说一说.
核心工作在 TableviewDatasource 的方法heightForRowAtIndexPath:中设置.
例子
假如这个 tableviewcell 中只有一个 UIlabel 充满整个 cell, 并且需要根据文本内容来确定 cell 的高度.
方法一
最普通的方法, 将赋值为当前 cell 的文本取出, 根据此文本获取自适应高度, 将此高度赋值给 cell 的 height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{NSString *text = textArr[indexPath.row]; //自适应高度CGSize autoSize = [text boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size; return autoSize.height;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //根据数据源 textArr配置 cell }
总结:
此方法是最普通的方法, 也是最基本的方法, 此方法还可以在封装优化.
方法二
将文本赋值给 cell时, 设置 cell 自身的高度, 这些代码也可以封装在 cell类的方法中. 然后在heightForRowAtIndexPath方法中, 根据 indexPath 获取当前 cell 获取 cell, 将 cell的 height 返回.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ LZMissionCell *cell = (LZMissionCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.height;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //根据数据源 textArr NSString *text = textArr[indexPath.row]; //自适应高度CGSize autoSize = [text boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size; //配置 cellLZMissionCell *cell = .......cell.height = autoSize.height; return cell;}
总结:
这个方法的优点是只用获取一次自适应高度, 但是通过实践, 发现当手动调用[self tableView:tableView cellForRowAtIndexPath:indexPath]方法时, tableview 还会重新配置 cell, 只是新的 cell 会将旧的 cell 覆盖了, 在 UI 上不会有异常可以看出. 但是, 如此的话, 这样会有一些性能和内存的损耗, 特定情况下会影响程序流畅性. 所以此方法最好不要使用.
方法三
此方法是方法一的封装版, 将 cell 的配置和文本自适应的方法都封装在 cell 类方法中. 在heightForRowAtIndexPath:中, 通过 indexPath 获取数据源 model 获取 cell 的自适应高度.
UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [LZMissionCell getTheAutoHeightForCellWithModel:_missionArr[indexPath.row]];}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //配置 cell LZMissionCell *cell = ....... cell.missionModel = modelArr[indexPath.row]; return cell;}
LZMissionCell.h
+ (CGFloat)getTheAutoHeightForCellWithModel:(LZMissionModel *)missionModel{ //cell 配置 model //根据数据源 modelArr NSString *text = modelArr[indexPath.row].text; //自适应高度CGSize autoSize = [text boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size; return autoSize.height;}
总结:
此方法是在 Controller 中最压力最小, 看起来也更加清晰.通过 cell 的类方法将获取, 例子中是平时开放中最常用的, 包括 model 和 cell 配置.所以此方法才是在 MVC 开发模式中最适合的.
- 65.TableViewCell 自适应高度最优方法
- iOS tableviewcell 高度自适应方法
- tableViewCell根据内容自适应高度的方法
- tableViewCell 自适应高度
- 自适应tableViewCell的高度
- tableViewcell高度自适应
- tableViewcell高度自适应
- TableViewCell自适应高度
- tableViewCell自适应高度
- TableViewCell高度的自适应
- [iOS] TableViewCell 自适应高度
- TableviewCell高度自适应
- tableViewCell 上面webView 自适应高度
- tableviewcell 自适应高度(模型数据自适应)
- TableViewCell自适应高度(异步下载)
- IOS-根据TableviewCell 总体高度 做自适应
- TableViewCell高度自适应的实现方案
- tableViewCell自适应高度(QQ聊天室模拟)
- 理解矩阵(三)
- js中使用encodeURIComponent编码url后java后台的解码
- 89. Gray Code
- HBuilder原生功能概述
- Pyhton: abs() 函数
- 65.TableViewCell 自适应高度最优方法
- android使用自定义字体
- hdu1024 Max Sum Plus Plus--DP
- android 调试时 出现的奇怪问题
- ImageSwitcher的用法
- QNAP NAS 搭建SVN服务器笔记
- Tomcat配置Server.xml文件,映射磁盘资源
- PHPstorm的快捷方式
- Linux USB 驱动开发(二)—— USB 驱动几个重要数据结构