我正在阅读初学iPhone开发手册&卡在第9章。我花了几个小时尝试调试此错误,但无济于事:
2010-05-01 19:27:51.361 Nav2[4113:20b] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709
2010-05-01 19:27:51.362 Nav2[4113:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
2010-05-01 19:27:51.364 Nav2[4113:20b] Stack: (
...
)
我不是在寻求本书的帮助,而是提示如何调试此错误。我可以确定哪些cellForRowAtIndexPath
方法是问题吗?以及如何检查类型?或者我应该看看其他什么?
编辑:根据要求,两个可疑方法的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *RootViewControllerCell = @"RootViewControllerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell];
if (cell == nil) {
cell == [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell] autorelease];
}
NSUInteger row = [indexPath row];
SecondLevelViewController *controller = [controllers objectAtIndex:row];
cell.textLabel.text = controller.title;
cell.image = controller.rowImage;
return cell;
}
-
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * DisclosureButtonCellIdentifier =
@"DisclosureButtonCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
DisclosureButtonCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: DisclosureButtonCellIdentifier]
autorelease];
}
return cell;
NSUInteger row = [indexPath row];
NSString *rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
[rowString release];
return cell;
}
答案 0 :(得分:5)
你有一个额外的等号,即:
cell == [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell] autorelease];
应该是:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell] autorelease];
答案 1 :(得分:1)
最好的方法之一是在所有cellForRowAtIndexpath:方法的第一行设置断点,然后在启用调试的情况下运行代码。做任何事情都会调用异常,你应该快速进入你的方法。
进行调试后,请逐步完成该方法。您可以使用“po objectname”获取有关各种对象的信息,因为它们是在方法中引用的。从控制台调用是很简单的,当您逐步执行代码时,只需将鼠标悬停在变量上就可以获得所需的内容。
猜测,你没有从你的方法中返回UITableViewCell。