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 开发模式中最适合的.

0 0
原创粉丝点击