15、iOStableview的常用delegate和dataSource执行顺序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

15、iOStableview的常用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)numberOfSectionsInT ableView:(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的个数,高度,内容来进行自动布局。

相关文档
最新文档