dequeueReusableCellWithIdentifier:forIndexPath 中断言失败:

2024-03-08

所以我正在为我的学校制作一个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 读者[4390:c07]* 断言失败 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 2012-10-04 20:13:05.357 读者[4390:c07] *由于未捕获而终止应用程序 异常“NSInternalInconsistencyException”,原因:“无法 将具有标识符 Cell 的单元出列 - 必须注册一个 nib 或一个类 用于标识符或连接故事板中的原型单元'*第一个抛出调用堆栈:(0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca19b 0x6692d 0x10e26b0 0x228dfc0 0x228233c 0x228聋子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:终止调用并抛出异常

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

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

请帮忙!


您正在使用dequeueReusableCellWithIdentifier:forIndexPath:方法。这文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:forIndexPath:对于该方法是这样说的:

重要的:您必须使用以下方法注册类或 nib 文件registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:方法之前调用该方法。

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

查看您的代码,您似乎期望出队方法返回nil如果它没有手机可以给你。您需要使用dequeueReusableCellWithIdentifier:对于该行为:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

请注意dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath:是不同的方法。请参阅文档前者 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier: and 后者 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:forIndexPath:.

如果您想了解为什么要使用dequeueReusableCellWithIdentifier:forIndexPath:, 看看这个问答 https://stackoverflow.com/q/25826383/77567.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dequeueReusableCellWithIdentifier:forIndexPath 中断言失败: 的相关文章

随机推荐