dequeueReusableCellWithIdentifier:forIndexPath 中的断言失败: [英] Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

查看:30
本文介绍了dequeueReusableCellWithIdentifier:forIndexPath 中的断言失败:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我为我的学校制作了一个 rss 阅读器并完成了代码.我运行了测试,它给了我那个错误.这是它所指的代码:

So I was making an rss reader for my school and finished the code. I ran the test and it gave me that error. Here is the code it's referring to:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                     forIndexPath:indexPath];
    if (cell == nil) {

        cell = 
         [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
                                reuseIdentifier:CellIdentifier];

    }

这是输出中的错误:

2012-10-04 20:13:05.356 Reader[4390:c07] * 断言失败-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 2012-10-0420:13:05.357 Reader[4390:c07] * 由于未捕获而终止应用程序异常NSInternalInconsistencyException",原因:无法使带有标识符 Cell 的单元出列 - 必须注册一个笔尖或一个类用于标识符或连接故事板中的原型单元'* 第一次抛出调用栈:(0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca669b20d 0xca19b20d0x228dfc0 0x228233c 0x228deaf 0x1058cd 0x4e1a6 0x4ccbf 0x4cbd9 0x4be340x4bc6e 0x4ca29 0x4f922 0xf9fec 0x46bc4 0x47311 0x2cf3 0x137b7 0x13da70x14fab 0x26315 0x2724b 0x18cf8 0x1becdf9 0x1becad0 0x1c06bf50x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x147da 0x1665c 0x2a02 0x2935)libc++abi.dylib:终止调用抛出异常

2012-10-04 20:13:05.356 Reader[4390:c07] * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 2012-10-04 20:13:05.357 Reader[4390:c07] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack: (0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca19b 0x6692d 0x10e26b0 0x228dfc0 0x228233c 0x228deaf 0x1058cd 0x4e1a6 0x4ccbf 0x4cbd9 0x4be34 0x4bc6e 0x4ca29 0x4f922 0xf9fec 0x46bc4 0x47311 0x2cf3 0x137b7 0x13da7 0x14fab 0x26315 0x2724b 0x18cf8 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x147da 0x1665c 0x2a02 0x2935) libc++abi.dylib: terminate called throwing an exception

这是它在错误屏幕中显示的代码:

and here's the code it shows in the error screen:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

请帮忙!

推荐答案

您正在使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法.文档 是这样说的:

You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. The documentation for that method says this:

重要提示:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier: 方法注册一个类或 nib 文件.

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

您没有为重用标识符 "Cell" 注册笔尖或类.

You didn't register a nib or a class for the reuse identifier "Cell".

查看您的代码,您似乎希望 dequeue 方法返回 nil 如果它没有提供给您的单元格.您需要使用 dequeueReusableCellWithIdentifier: 来实现该行为:

Looking at your code, you seem to expect the dequeue method to return nil if it doesn't have a cell to give you. You need to use the dequeueReusableCellWithIdentifier: for that behavior:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

注意 dequeueReusableCellWithIdentifier:dequeueReusableCellWithIdentifier:forIndexPath: 是不同的方法.请参阅文档 前者后者.

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods. See doc for the former and the latter.

如果您想了解为什么要使用 dequeueReusableCellWithIdentifier:forIndexPath:请查看这个问答.

If you want to understand why you'd want to ever use dequeueReusableCellWithIdentifier:forIndexPath:, check out this Q&A.

这篇关于dequeueReusableCellWithIdentifier:forIndexPath 中的断言失败:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆