XCode iOS - 断言失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25231391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode iOS - Assertion Failure
提问by Jonathan Cakes
I'm just starting out to learn iOS development. I'm doing the “Start Developing iOS Apps Today” tutorial and I'm stuck on the section “Add Data”.
我刚刚开始学习 iOS 开发。我正在做“今天开始开发 iOS 应用程序”教程,但我被困在“添加数据”部分。
After setting the Table view to use ‘Dynamic Prototypes', and setting the identifier to ‘ListPrototypeCell', I added the method ‘cellForRowAtIndexPath' but it's crashing with these errors:
在将 Table 视图设置为使用“Dynamic Prototypes”并将标识符设置为“ListPrototypeCell”后,我添加了“cellForRowAtIndexPath”方法,但它因以下错误而崩溃:
Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2014-08-10 13:35:50.519 ToDoList[8954:60b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439
2014-08-10 13:35:50.523 ToDoList[8954:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ListPrototypeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
. . . .
libc++abi.dylib: terminating with uncaught exception of type NSException
Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle>(未加载)
2014-08-10 13:35:50.519 ToDoList[8954:60b] *** 断言失败 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:54
2014-08-10 13:35:50.523 ToDoList[8954:60b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符 ListPrototypeCell 的单元出列-必须为标识符或连接故事板中的原型单元'
. . . .
libc++abi.dylib:以未捕获的 NSException 类型异常终止
I've been following the tutorial exactly and I can't find the mistake. Can anyone suggest what I'm doing wrong?
我一直在完全按照教程进行操作,但找不到错误。谁能建议我做错了什么?
The code:
编码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
return cell;
}
回答by Rikkles
Use:
用:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell"];
For storyboards that's what you want. Also follow it up if necessary with:
对于故事板,这就是您想要的。如有必要,还可以通过以下方式跟进:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListPrototypeCell"];
}
Edit: If instead, you want to continue using dequeueReusableCellWithIdentifier:forIndexPath:
, then in your controller's initialization you should use registerClass:forCellReuseIdentifier:
, as per the docs if you are not using a storyboard. Also the (cell == nil) part has become unnecessary in the latest Xcode versions.
编辑:如果相反,您想继续使用dequeueReusableCellWithIdentifier:forIndexPath:
,那么在您的控制器初始化中,您应该使用registerClass:forCellReuseIdentifier:
,如果您不使用故事板,则按照文档。在最新的 Xcode 版本中, (cell == nil) 部分也变得不必要了。