dequeueReusableCellWithIdentifier:forIndexPath中的断言失败:

本文翻译自:Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

So I was making an rss reader for my school and finished the code. 因此,我正在为我的学校准备一个RSS阅读器,并完成了代码。 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];

    }

here's the error in the output: 这是输出中的错误:

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 2012-10-04 20:13:05.356 Reader [4390:c07] *-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/ SourceCache / UIKit_Sim / UIKit-2372 / UITableView.m:4460 2012-10-04 20中的断言失败: 13:05.357 Reader [4390:c07] *由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为Cell的单元出队-必须为该标识符注册一个笔尖或一个类,或者在情节提要中连接原型单元“*第一掷调用堆栈:(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:终止调用引发异常

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]));
    }
}

please help! 请帮忙!


#1楼

参考:https://stackoom.com/question/rRhM/dequeueReusableCellWithIdentifier-forIndexPath中的断言失败


#2楼

You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. 您正在使用dequeueReusableCellWithIdentifier:forIndexPath:方法。 The documentation for that method says this: 该方法的文档说明:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method. 重要提示:在调用此方法之前,必须使用registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:方法注册类或nib文件。

You didn't register a nib or a class for the reuse identifier "Cell" . 您没有为重用标识符"Cell"注册笔尖或类。

Looking at your code, you seem to expect the dequeue method to return nil if it doesn't have a cell to give you. 查看您的代码,您似乎希望dequeue方法在没有单元格的情况下返回nil You need to use the dequeueReusableCellWithIdentifier: for that behavior: 您需要针对该行为使用dequeueReusableCellWithIdentifier: ::

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods. 请注意, dequeueReusableCellWithIdentifier:dequeueReusableCellWithIdentifier:forIndexPath:是不同的方法。 See doc for the former and the latter . 前者后者参见doc。

If you want to understand why you'd want to ever use dequeueReusableCellWithIdentifier:forIndexPath: , check out this Q&A . 如果您想了解为什么要使用dequeueReusableCellWithIdentifier:forIndexPath: dequeueReusableCellWithIdentifier:forIndexPath: 此问答


#3楼

I'll just add that Xcode 4.5 includes the new dequeueReusableCellWithIdentifier:forIndexPath: 我将添加Xcode 4.5包括新的dequeueReusableCellWithIdentifier:forIndexPath:
in its default template code - a potential gotcha for developers expecting the older dequeueReusableCellWithIdentifier: method. 在其默认模板代码中-对于期望使用较旧的dequeueReusableCellWithIdentifier:方法的开发人员来说,这是一个潜在的难题。


#4楼

i had the same problem replacing with 我有同样的问题替换

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

solved 解决了


#5楼

I think this error is about registering your nib or class for the identifier. 我认为此错误与注册您的笔尖或类作为标识符有关。

So that you may keep what you are doing in your tableView:cellForRowAtIndexPath function and just add code below into your viewDidLoad: 这样您就可以在tableView:cellForRowAtIndexPath函数中保留所做的工作,而只需将以下代码添加到viewDidLoad中:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

It's worked for me. 对我有用。 Hope it may help. 希望对您有所帮助。


#6楼

In your storyboard you should set the 'Identifier' of your prototype cell to be the same as your CellReuseIdentifier "Cell". 在情节提要中,应将原型单元的“标识符”设置为与CellReuseIdentifier“单元”相同。 Then you won't get that message or need to call that registerClass: function. 然后,您将不会收到该消息或需要调用该registerClass:函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值