UITableView三个主要方法的调用顺序和次数问题

寂寞流萤520 2017-07-07 11:07:04
为了验证TableView的方法调用的顺序,使用打印来验证
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"一共有多少组 --- numberOfSectionsInTableView");
return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection --- %ld组", section);

if (section == 0) {
return 2;
} else if (section == 1){
return 3;
} else {
return 1;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath --- %ld组 %ld行", indexPath.section, indexPath.row);

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"第0组 - 第0行";
} else if (indexPath.row == 1) {
cell.textLabel.text = @"第0组 - 第1行";
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"第1组 - 第0行";
} else if (indexPath.row == 1) {
cell.textLabel.text = @"第1组 - 第1行";
} else if (indexPath.row == 2) {
cell.textLabel.text = @"第1组 - 第2行";
}
} else if (indexPath.section == 2) {
cell.textLabel.text = @"第2组 - 第0行";
}

return cell;
}


控制台输出语句
2017-07-07 10:57:06.190 UITableView[13866:3063529] 一共有多少组 --- numberOfSectionsInTableView
2017-07-07 10:57:06.191 UITableView[13866:3063529] numberOfRowsInSection --- 2组
2017-07-07 10:57:06.191 UITableView[13866:3063529] numberOfRowsInSection --- 0组
2017-07-07 10:57:06.191 UITableView[13866:3063529] numberOfRowsInSection --- 1组
2017-07-07 10:57:06.191 UITableView[13866:3063529] 一共有多少组 --- numberOfSectionsInTableView
2017-07-07 10:57:06.192 UITableView[13866:3063529] numberOfRowsInSection --- 2组
2017-07-07 10:57:06.192 UITableView[13866:3063529] numberOfRowsInSection --- 0组
2017-07-07 10:57:06.192 UITableView[13866:3063529] numberOfRowsInSection --- 1组
2017-07-07 10:57:06.195 UITableView[13866:3063529] 一共有多少组 --- numberOfSectionsInTableView
2017-07-07 10:57:06.195 UITableView[13866:3063529] numberOfRowsInSection --- 2组
2017-07-07 10:57:06.195 UITableView[13866:3063529] numberOfRowsInSection --- 0组
2017-07-07 10:57:06.196 UITableView[13866:3063529] numberOfRowsInSection --- 1组
2017-07-07 10:57:06.196 UITableView[13866:3063529] cellForRowAtIndexPath --- 0组 0行
2017-07-07 10:57:06.209 UITableView[13866:3063529] cellForRowAtIndexPath --- 0组 1行
2017-07-07 10:57:06.210 UITableView[13866:3063529] cellForRowAtIndexPath --- 1组 0行
2017-07-07 10:57:06.211 UITableView[13866:3063529] cellForRowAtIndexPath --- 1组 1行
2017-07-07 10:57:06.211 UITableView[13866:3063529] cellForRowAtIndexPath --- 1组 2行
2017-07-07 10:57:06.212 UITableView[13866:3063529] cellForRowAtIndexPath --- 2组 0行


有几点不明白
有疑惑的代码如下:
一共有多少组 --- numberOfSectionsInTableView
numberOfRowsInSection --- 2组
numberOfRowsInSection --- 0组
numberOfRowsInSection --- 1组
疑惑点
1、这几行为何全部都重复调用了多次
2、numberOfRowsInSection不应该按照0组、1组、2组的顺序调用吗
...全文
373 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
寂寞流萤520 2018-09-21
  • 打赏
  • 举报
回复
既然没问题,那原理是什么呢
Quinn士魁 2017-07-14
  • 打赏
  • 举报
回复
疑惑点 2、numberOfRowsInSection不应该按照0组、1组、2组的顺序调用吗 不是的 log信息没问题

29,030

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧