UITableView 加载时候,代理方法执行顺序
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfSectionsInTableView");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection");
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
NSLog(@"cellForRowAtIndexPath");
}
NSLog(@"cellForRowAtIndexPath01");
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"heightForRowAtIndexPath");
return 40;
}
初次加载视图,执行顺序
2017-06-14 22:42:43.095 oc1[6631:246491] numberOfSectionsInTableView
2017-06-14 22:42:43.095 oc1[6631:246491] numberOfRowsInSection
2017-06-14 22:42:43.096 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.096 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.096 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.426 oc1[6631:246491] numberOfSectionsInTableView
2017-06-14 22:42:43.426 oc1[6631:246491] numberOfRowsInSection
2017-06-14 22:42:43.427 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.427 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.427 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.434 oc1[6631:246491] numberOfSectionsInTableView
2017-06-14 22:42:43.435 oc1[6631:246491] numberOfRowsInSection
2017-06-14 22:42:43.435 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.435 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.435 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.437 oc1[6631:246491] cellForRowAtIndexPath
2017-06-14 22:42:43.437 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:42:43.438 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.445 oc1[6631:246491] cellForRowAtIndexPath
2017-06-14 22:42:43.445 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:42:43.446 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:42:43.447 oc1[6631:246491] cellForRowAtIndexPath
2017-06-14 22:42:43.447 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:42:43.447 oc1[6631:246491] heightForRowAtIndexPath
滑动cell 时候,执行顺序
2017-06-14 22:45:16.497 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:45:16.498 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:45:16.580 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:45:16.581 oc1[6631:246491] heightForRowAtIndexPath
2017-06-14 22:45:16.664 oc1[6631:246491] cellForRowAtIndexPath01
2017-06-14 22:45:16.665 oc1[6631:246491] heightForRowAtIndexPath
观察可见,初次加载时候先调用heightForRowAtIndexPath方法给默认cell 一个高度,当创建cell 时候,调用了cellForRowAtIndexPath之后又调用了heightForRowAtIndexPath 重新设置了一下高度。当我们在屏幕中滑动cell时候,重用已经存在的cell,不再创建新cell,并且是先调用了cellForRowAtIndexPath01 然后再调用heightForRowAtIndexPath 方法,给cell重新设置高度。