所以我为我的学校做了一个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]));
    }
}

请帮助!


当前回答

我只会添加Xcode 4.5包含新的dequeueReusableCellWithIdentifier:forIndexPath: 在它的默认模板代码中-对于希望使用旧的dequeueReusableCellWithIdentifier:方法的开发人员来说,这是一个潜在的问题。

其他回答

遇到这个错误bc单元复用标识符是错误的-一个新手错误,但它发生了: 1. 确保单元格重复使用标识符没有拼写错误或缺少字母。 2. 同样的,不要忘记大写的重要性。 3.0不是O(哦)

我在Objective C和Swift中都给出了答案。在这之前我想说

如果我们使用dequeueReusableCellWithIdentifier:forIndexPath:,我们必须在调用这个方法as之前使用registerNib:forCellReuseIdentifier:或registerClass:forCellReuseIdentifier:方法注册一个类或nib文件 苹果文档说

所以我们添加registerNib:forCellReuseIdentifier:或registerClass:forCellReuseIdentifier:

一旦我们为指定的标识符注册了一个类,并且必须创建一个新的单元格,这个方法通过调用它的initWithStyle:reuseIdentifier:方法来初始化单元格。对于基于nib的单元格,此方法从提供的nib文件加载cell对象。如果已有的单元格可供重用,则此方法转而调用该单元格的prepareForReuse方法。

在viewDidLoad方法中,我们应该注册单元格

Objective - C

选项1:

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

选项2:

[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"cell"];

在上面的代码nibWithNibName:@“CustomCell”给你的nib名字,而不是我的nib名字CustomCell

斯威夫特

选项1:

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

选项2:

tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") 

在上面的代码nibName:"NameInput"给出你的nib名称

我有同样的问题,有同样的错误,对我来说,它是这样工作的:

[self.tableView registerNib:[UINib nibWithNibName:CELL_NIB_HERE bundle: nil] forCellReuseIdentifier:CELL_IDENTIFIER_HERE];

也许它会对其他人有用。

在我的情况下,崩溃发生时,我调用deselectrowatindexpath:

这一行是[tableView deselectRowAtIndexPath:indexPath animated:YES];

把它改成[self。tableView deselectRowAtIndexPath:indexPath animated:YES];解决了我的问题!

希望这对大家有所帮助

FWIW,当我忘记在故事板中设置单元格标识符时,我得到了同样的错误。如果这是您的问题,那么在故事板中单击表格视图单元格,并在属性编辑器中设置单元格标识符。确保您在这里设置的单元格标识符与

static NSString *CellIdentifier = @"YourCellIdenifier";