iOS tableview的常用delegate和dataSource执行顺序

今天在做项目的时候,项目中是使用两个section的,第一个section里面只有一组数据,也就是一行cell。第二个section里面cell的行数根据数据源数组的count来决定。我做的时候想着既然我第一个section知道他就是一行cell,那我直接在

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

这个方法中 当section == 1的时候 return 1; 然后我发现我给自己挖了一个大坑,在给cell赋值的时候,发现每次走到 cell.model = [arr objectAtIndex:indexPath.row]; 这里的时候就会崩掉,控制台显示我往一个空的数组中传值了。我从头到尾看了一遍,最后发现就是因为在设置numberOfRowsInSection的时候 将第一个section ==1 的时候让它定死了,而我的数据是从网络请求的,可能是因为数组中还没有数据,所以在去数据的时候就会数组越界。由此我想到了要了解一下tableview的几个常用代理方法的执行先后顺序,一遍以后不再马虎犯错。

#pragma mark - Table view data source  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  
先看有几个section 默认1个
  
#pragma mark - Table view delegate  
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;  
标题头的高度
  
#pragma mark - Table view data source  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;  
*******每个section中有多少行cell  

#pragma mark - Table view delegate  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;  
  ********每个cell的行高

#pragma mark - Table view data source  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;  
*********初始化每个cell的内容  

#pragma mark - Table view delegate  
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 
其实我们最经常用的就是numberOfRowsInSection  每个section中有多少行cell
heightForRowAtIndexPath   每个cell的高度
cellForRowAtIndexPath    每个cell中的内容  
tableview 根据cell的个数,高度,内容来进行自动布局
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容