【问题标题】:Assertion error when using pushViewController to a TableViewController将 pushViewController 用于 TableViewController 时的断言错误
【发布时间】:2012-11-20 10:34:38
【问题描述】:

我正在尝试使用以下代码以编程方式从视图控制器转换到表视图控制器(与情节提要 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

【问题讨论】:

  • 完整的错误是什么?

标签: iphone ios segue pushviewcontroller


【解决方案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];
}

【讨论】:

  • 感谢您的回复。由于我确实需要稍后在该方法中访问 indexPath,因此看起来我需要实现 registerNib/registerClass。如果在使用 segue 进行转换时仍然需要保留功能,我应该同时实现这两种注册方法吗?
  • 即使你只使用 [tableView dequeueReusableCellWithIdentifier:CellIdentifier],你仍然可以访问 indexPath,tableView:cellForRowAtIndexPath: 仍然给你 indexPath。您将实现 registerNib/registerClass 取决于您在做什么,您是创建 UITableViewCell 的子类,还是只是使用 NIB 文件对单元格进行原型制作,但我仍然认为您可以只使用 [tableView dequeueReusableCellWithIdentifier:CellIdentifier]。
  • 好的,只使用那个方法,原来的错误现在就消失了。谢谢!但是,它确实在这里跳闸:-[UITableView _configureCellForDisplay:forIndexPath:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
  • 好的,太好了——它有效。表格视图确实成功出现,因此回答了我原来的问题。当我以编程方式转换时,我在情节提要中设置的 UIBarButtonItem 和 segue 不再起作用......我开始看到使用情节提要的缺点(或者至少在我使用它们的方式上)。
猜你喜欢
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多