cellForRowAtIndexPath 中奇怪的跳转到另一个 return 语句
我的 tableView:cellForRowAtIndexPath:
中有这个,
if (condition) {
// make a custom cell
return cell; // line number 240
}
else {
// make another kind of cell
return cell; // line number 256
}
我得到了 EXC_BAD_ACCESS,所以我启动了调试器并设置了一个断点,逐行单步执行代码。
当我从第 256 行向前“跳过”时,它直接进入第 240 行,即进入 if 语句的另一个分支。这怎么可能???
非常感谢您的帮助,
萨沙
I have this in my tableView:cellForRowAtIndexPath:
if (condition) {
// make a custom cell
return cell; // line number 240
}
else {
// make another kind of cell
return cell; // line number 256
}
I got a EXC_BAD_ACCESS, so I fired up the debugger and set a breakpoint, stepping through the code line by line.
When I stepped forward "step over" from line number 256 it went directly to line 240, i.e. into another branch of the if
statement. How is that possible???
Thanks a lot for your help,
Sascha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我找到了解决方案。具有表视图的视图控制器也是后台 Web 加载线程的委托。显然,调试器一次只能单步执行一个线程。委托例程确实调用了
scrollToRowAtIndexPath:
而没有检查上述条件
。不过,我发现调试器的行为有点令人费解。
I think I found the solution. The view controller with the table view is also the delegate of a background web loading thread. Apparently the debugger can only step through one thread at a time. The delegate routine did indeed call a
scrollToRowAtIndexPath:
without checking the abovecondition
.Still, I find the behavior of the debugger a bit puzzling.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
将被调用 x 次,其中 x = 行数。所以我认为你的问题是你的
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
没有返回正确的值,并且很可能是一个大于你所在位置的值重新从中提取数据。即,如果您有一个大小为 3 的数组,但尝试创建第 4 行单元格。
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
will get called x times, where x = number of rows.So I think your problem is that your
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
doesn't return a correct value, and very likely a value that's greater than where you're pulling data from.i.e. If you have a array of size 3, but trying to make the 4th row cell.
此方法调用自身的次数与
numberOfRowsInSection
方法中返回的行数以及每次单元格为 nil 的次数相同。最有可能的是,控件转到下一行为其分配UITableViewCell
,当时,我猜测if
条件已得到满足。This method calls itself the same number of times as the number of rows returned in the
numberOfRowsInSection
method and each time a cell is nil. Most likely, the control went to the next row to allocate theUITableViewCell
for it, and at that time, I am guessing that theif
condition got satisfied.Step Over 与 Step Into 相同,只不过当它到达另一个过程的调用时,它不会单步进入该过程。该过程将运行,并且您将进入当前过程中的下一条语句。
因此,请跳过获取下一条语句和 cellForRowAtIndexPath 调用 numberOfRow 次。因此,在某些迭代中,您的 if 条件变为 true 并转到您的第 240 行
Step Over is the same as Step Into, except that when it reaches a call for another procedure, it will not step into the procedure. The procedure will run, and you will be brought to the next statement in the current procedure.
so step over get the next statement and your cellForRowAtIndexPath call numberOfRow times. so in some iteration your if condition becomes true and goes to your line no 240