对于cell高度固定的页面来说这个很好办直接在heightForRowAtIndexPath方法中返回固定的一个数值就行。然而对于cell高度不固定的页面来说却需要我们开发人员计算出cell的高度。 笔者之前通常会会将计算cell高度的代码放在heightForRowAtIndexPath方法中,看代码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ TopicModel * model = self.dataSource[indexPath.row return cellHeight; } 上面的代码虽然能计算出cell的高度实现想要的效果,可会有一个耗性能的问题:即使之前计算过某个cell的高度,在这个cell在此展示出来的时候还会再次调用heightForRowAtIndexPath 代码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
方法会重复执行好多次,首先,并且heightForRowAtIndexPath方法的执行机制在不同版本的iOS系统还会有很大不同。 以iOS9为例,一行cell要展示在屏幕上,至少要执行5遍TableView的heightForRowAtIndexPath方法: TableView配置部分: ① 当TableView视图即将展现在屏幕上时 二、对UITableView可变行高的计算方式进行优化 通过前面的分析,可以理解如果将复杂的计算代码写在heightForRowAtIndexPath方法中,代价将是非常惨重的。 这个值设置之后,开发者无需设置rowHeight属性,也不需要实现heightForRowAtIndexPath方法,系统会自动根据UITableViewCell中contentView的约束来计算自己的行高 方法,heightForRowAtIndexPath方法会以懒加载的方式执行,只有在cell将要展现在屏幕上时heightForRowAtIndexPath方法才会被执行,这也可以有效减小由于高度计算带来的性能负担
PingjiaTableViewCell; self.offscreenCells["PingjiaTableViewCell"] = cell; 计算高度 func tableView(tableView: UITableView, heightForRowAtIndexPath self.tableView.rowHeight = UITableViewAutomaticDimension; 去掉下面的代理方法 func tableView(tableView: UITableView, heightForRowAtIndexPath
2)减少heightForRowAtIndexPath代理中的计算量(cell的高度计算)。 3.2 减少heightForRowAtIndexPath代理中的计算量: ① 由于每次tableView进行update(更新)都会对每一个cell调用heightForRowAtIndexPath代理取得最新的 如果表格的所有cell高度都是固定的,那么去掉heightForRowAtIndexPath代理,直接设置tableView的rowHeight属性为固定的高度。
此时heightForRowAtIndexPath方法中的代码为: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
mainScreen].bounds.size.width - 20; } 设置tableView的cell估算高度 // 告诉tableView所有cell的估算高度(设置了估算高度,就可以减少tableView:heightForRowAtIndexPath self.tableView.estimatedRowHeight = 200; 在代理方法中计算cell的高度 XMGStatusCell *cell; - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
如下面是计算UITableView高度的代码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath indexPath.row]; [cell.t sizeToFit]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath indexPath.row]; [cell.t sizeToFit]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath return cell; } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
.可能你会说: 顶部给个非微博正文区域给个固定高度;文字区域动态计算出高度;图片部分,图片高度固定,根据数量动态计算高度;转发部分同理;然后根据数据在tabelView的代理方法 tableView:heightForRowAtIndexPath 但是又要求完全显示的设计,最复杂的不在于实现,而在于后期的迭代.可变字段越多,迭代越复杂.如果连显示方式都改了,那就基本等于重做了几遍. cell高度计算有坑: 难以理解的诡异问题 在 tableView:heightForRowAtIndexPath 关于这个话题,比较易犯的错误是,竟然有开发者在 tableView:heightForRowAtIndexPath: 中调用 tableView:cellForRowAtIndexPath: 来获取cell 核心代码片段: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
= 首先要确定的是,在iOS中,系统会先调用“tableView:heightForRowAtIndexPath:”获取每个Cell即将显示的高度,从而确定整个UITableView的布局。 property(assign, nonatomic) CGFloat cellHeight; //计算高度 (void)calculateCellHeight; @end 这样,就不用在“tableView:heightForRowAtIndexPath
UITableViewCellSelectionStyleNone; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath UITableViewCellSelectionStyleNone; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath 重要的事情说三遍… (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
endUpdates]; [self scrollToBottom:YES]; } image.png 代码 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
sharedApplication].idleTimerDisabled = YES; 6.隐藏某行cell - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
numberOfRowsInSection:(NSInteger)section{ return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
优化就注意几点 1.TableView性能影响最大的是每个cell高度的获heightForRowAtIndexPath,这个函数会调用很多次,对于不是固定高度的cell,每次都需要去计算。
代理实现 2.1 必须实现代理方法 /// collectionItem高度 func heightForRowAtIndexPath(collectionView collection: UICollectionView
NSIndexPath) { print("\(indexPath.row)") } func tableView(tableView: UITableView, heightForRowAtIndexPath
在这个方法中返回indexPath位置对应cell的高度 /** * 返回每一行cell的具体高度 */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
+10+row*30; cell.frame = rect; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
numberOfRowsInSection:(NSInteger)section { return 1;} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath