ios - dequeueReusableCellWithIdentifier :forIndexPath: 中的断言失败

标签 ios objective-c uitableview

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

- (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] * 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

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

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

请帮忙!

最佳答案

您正在使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法。 documentation因为那个方法是这样说的:

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

您没有为重用标识符 "Cell" 注册 nib 或类。

查看您的代码,您似乎希望 dequeue 方法返回 nil 如果它没有单元格可以给您。您需要为该行为使用 dequeueReusableCellWithIdentifier::

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

注意 dequeueReusableCellWithIdentifier:dequeueReusableCellWithIdentifier:forIndexPath: 是不同的方法。见文档 the formerthe latter .

如果您想了解为什么要使用 dequeueReusableCellWithIdentifier:forIndexPath:, check out this Q&A .

关于ios - dequeueReusableCellWithIdentifier :forIndexPath: 中的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12737860/

相关文章:

ios - UITableViewController 中的 UITextField

ios - UITableViewCell selectedBackgroundView 自定义

ios - 当我尝试加载 AdMob 广告时 iPad 崩溃

objective-c - Cocoa:当我有一个 "id button"变量时如何获取按钮类型

ios -- 如何限制 CAEmitter 粒子轨迹的范围?

ios - NSNumberFormatter numberFromString 十进制数

ios - XCode 提示 viewDidLoad 和 "Missing @end"(iAd)

iphone - 自定义绘制的 UITableViewCell 中的可访问性

ios - SWIFT 线程 1 : EXC Bad instruction XCODE IOS

ios - NSStream 写入套接字不会在 WiFi 关闭时出现错误。为什么?