以前做过些项目,里面难免会遇到图文混排,Cell自适应高度的问题,当用到这些的时候,我们的一般做法是在cell的高度返回代理方法中算出Cell的高度,然后返回cell的高度.这其中就用到了cell得重用机制,于是想写一些关于Cell的重用机制的一些自己的理解.首先写了一个测试的Demo,贴代码
@implementation ViewController
{
UITableView *_table;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createUI];
}
- (void)createUI
{
_table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:_table];
_table.dataSource = self;
_table.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"row");
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",arc4random()%100+1];
NSLog(@"cell");
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"heigth");
return 100;
}
运行,打印结果如下:
2015-12-16 20:04:44.994 TestTableview[2728:2226549] row
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.999 TestTableview[2728:2226549] row
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] row
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.009 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.011 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.013 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.021 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.022 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.023 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.023 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.024 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.025 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.026 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.026 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.027 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.027 TestTableview[2728:2226549] heigth
发现Row被执行了三次,前两次为什么执行,目前还没有弄懂
![委屈](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
2015-12-16 20:04:45.008 TestTableview[2728:2226549] row
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.009 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.011 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.013 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.021 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.022 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.023 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.023 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.024 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.025 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.026 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.026 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.027 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.027 TestTableview[2728:2226549] heigth
这里面height被执行了16次,前面执行了10次,然后和cell交替执行了6次,加上代码
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sometime) userInfo:nil repeats:NO];
- (void)sometime
{
NSLog(@"visablecells count=%lu",_table.visibleCells.count);
}
打印结果
2015-12-16 20:20:38.562 TestTableview[2741:2230162] visablecells count=6
发现刚好可见的cells刚好是6个,而总过有10个cell,所以可以推断,tableview首先计算出总共数量的cell的高度(10个),这里是估算tableview的countofsize,然后先复用cell,根据计算cell的高度,直到cell将tableview尺寸(这里是整个屏幕)是塞满(visablecells这里是6个),滚动的时候,消失的visablecell(滚出tableview尺寸范围)将会进入复用池,当需要下个cell滚出来时,会先复用cell,在调用返回高度的方法确定cell的高度,这个复用过程就完成了.这就是cell自适应高度时,为什么在自定义cell里面根据内容算出cell的高度,并在代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
中返回cell的高度了.