iOS UITableView 代理方法运行顺序

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重新设置高度。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容