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

请帮助!


当前回答

使用Swift 3.0:

override func viewDidLoad() {
super.viewDidLoad()

self.myList.register(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
}



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myList.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! MyTableViewCell

return cell
}

其他回答

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

在Swift中,这个问题可以通过添加以下代码来解决

viewDidLoad

方法。

tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")

这个问题很可能是因为你在storyboard中配置了自定义的UITableViewCell,但是你没有使用storyboard来实例化你的UITableViewController,它使用这个UITableViewCell。例如,在MainStoryboard中,你有一个UITableViewController子类,叫做MyTableViewController,还有一个自定义动态UITableViewCell,叫做MyTableViewCell,标识符id为MyCell。

如果你像这样创建你的自定义UITableViewController:

 MyTableViewController *myTableViewController = [[MyTableViewController alloc] init];

它不会自动为你注册你的自定义tableviewcell。您必须手动注册它。

但是如果你使用storyboard来实例化MyTableViewController,像这样:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyTableViewController *myTableViewController = [storyboard  instantiateViewControllerWithIdentifier:@"MyTableViewController"];

好事发生了!UITableViewController会自动注册你在storyboard中定义的自定义tableview单元格。

在你的委托方法"cellForRowAtIndexPath"中,你可以像这样创建你的表格视图单元格:

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

//Configure your cell here ...

return cell;
}

如果循环队列中没有可重用的单元格,dequeueReusableCellWithIdentifier将自动为您创建新的单元格。

那么你就完成了!

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

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

也许它会对其他人有用。

对某些人来说,这可能很愚蠢,但我明白了。我得到了这个错误,我的问题是,我试图使用静态单元格,但动态添加更多的东西。如果你调用这个方法,你的单元格需要是动态原型。选择storyboard中的单元格,在Attributes检查器下,第一个内容是“Content”,你应该选择动态原型,而不是静态原型。