将pushViewController用于TableViewController时出现断言错误

时间:2012-12-02 23:29:55

标签: iphone ios segue pushviewcontroller

我正在尝试以编程方式从视图控制器转换到表视图控制器(与故事板segue相比),使用以下代码:

[self.navigationController pushViewController:photosTVC animated:YES];

然而,当表视图控制器加载时,我收到“断言错误”;具体来说,在这种方法的第二行:

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

表格视图控制器在使用segue转换到它时加载正常,但是当我以这种方式转换时不会。我可以尝试的任何想法都会非常感激!

感谢阅读。

编辑:完整错误文本如下:

断言失败 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/ SourceCache / UIKit_Sim / UIKit-2372 / UITableView.m:4460

1 个答案:

答案 0 :(得分:1)

要使用[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];,必须使用registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:注册该单元格,如果您使用的是故事板,则必须为您注册。

因此,除非有特殊原因,否则请以编程方式使用此方法[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

编辑:

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    // or whatever cell initialisation method you have / created
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
相关问题