//
// TableViewController.h
// AdaptiveCell
//
// Created by swinglife on 14-1-10.
// Copyright (c) 2014年 swinglife. All rights reserved.
//
#import
@interface TableViewController : UIViewController
}
@property (nonatomic,retain) UITableView *tableView;
@end
tableview cell自适应高度的问题:
UITableViewCell高度计算
rowHeight
UITableView是开发者再熟悉不过的视图了,它的 delegate 和 data source 回调也是经常使用的,也难免遇到 UITableViewCell 高度计算的事。
UITableView 询问 cell 高度有两种方式。
一种是针对所有 Cell 具有固定高度的情况,通过:
self.tableView.rowHeight = 88;
上面的代码指定了一个所有 cell 都是 88 高度的 UITableView,对于定高需求的表格,强烈建议使用这种(而非下面的)方式保证不必要的高度计算和调用。rowHeight属性的默认值是 44,所以一个空的 UITableView 显示成那个样子。
另一种方式就是实现 UITableViewDelegate 中的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return xxx
}
需要注意的是,实现了这个方法后,rowHeight 的设置将无效。所以,这个方法适用于具有多种 cell 高度的 UITableView。
设置估算高度后,contentSize.height 根据“cell估算值 x cell个数”计算,这就导致滚动条的大小处于不稳定的状态,contentSize 会随着滚动从估算高度慢慢替换成真实高度,肉眼可见滚动条突然变化甚至“跳跃”。