当UITableVC包含在另一个VC中时,找不到单元格标识符



我试图创建一个带有一些自定义单元格的表视图,但遇到了一个问题。一切都设置正确,当我使用这个UITableVC作为初始VC时,一切都正常。但当我试图将它作为一个子VC添加到另一个VC时,我会得到以下错误:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460
2013-02-05 18:37:25.704 SalesBot[16284:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Statement Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我没有改变任何其他东西,我只是将故事板中的箭头移动到另一个VC,使其初始。

以下是我如何将UITableVC子类作为子类添加到另一个VC:

self.statementTableViewController = [[SBStatementTableViewController alloc] init];
[self.view addSubview:self.statementTableViewController.view];
[self.statementTableViewController.view setFrame:self.contentFrame];

下面是我如何将一个单元格排成队列:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Statement Cell";
    SBStatementCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    return cell;
}

我知道上面的代码只是iOS6,我稍后会担心iOS5:)

我猜单元格标识符在某种程度上不是"全局"的,当它是另一个VC的子代时,表VC看不到它们?

如果您需要查看更多代码,请帮助并让我知道!

尽管您还没有跟进您的代码,但我敢打赌这是您的问题。

取自:出队列中的断言失败ReusableCellWithIdentifier:forIndexPath:

您正在使用dequeueReusableCellWithIdentifier:forIndexPath:方法。该方法的文档是这样写的:

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

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

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

请注意,dequeueReusableCellWithIdentifier:和dequeueReisableCellWithidentifier:forIndexPath:是不同的方法。

最新更新