从 tableviewcontroller 加载 tableviewcontroller 不起作用

Posted

技术标签:

【中文标题】从 tableviewcontroller 加载 tableviewcontroller 不起作用【英文标题】:Loading a tableviewcontroller from a tableviewcontroller not working 【发布时间】:2012-08-03 09:49:32 【问题描述】:

我创建了一个基于视图的应用程序,并在其中添加了两个表视图控制器。这是我按下 Viewcontroller 按钮的事件流-->加载表视图控制器,如果选择了一个单元格-->显示第二个表控制器。

这是我的代码。在 Viewcontroller.m 中

-(IBAction)product:(id)sender
 
Products *sec=[[Products alloc]init];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:sec];


[self presentModalViewController:nav animated:YES];    

在我的第一个名为 products.m 的 Tableview 控制器中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

// Navigation logic may go here. Create and push another view controller.

 Description *detailViewController = [[Description alloc] initWithNibName:@"Description" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];




 [detailViewController release];


我的第二个 TableView 控制器命名为描述。现在问题是我的第二个 tableview 控制器没有显示,我遇到的问题是

 *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:],     /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
 2012-08-03 15:10:51.016 jothi R&D[1112:f803] *** Terminating app due to uncaught  exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a  cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x13d3022 0x1564cd6 0x137ba48 0x9b42cb 0xb7d28 0xb83ce 0xa3cbd 0xb26f1 0x5bd21 0x13d4e42  0x1d8b679 0x1d95579 0x1d1a4f7 0x1d1c3f6 0x1d1bad0 0x13a799e 0x133e640 0x130a4c6 0x1309d84  0x1309c9b 0x12bc7d8 0x12bc88a 0x1d626 0x22a2 0x2215 0x1)
terminate called throwing an exception

但是,如果我尝试通过替换 products.m 中的描述来显示 Viewcontroller,它可以工作.. 请指导我...

【问题讨论】:

reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' *** First throw call stack:这是你的问题。`你应该总是在cellForRowAtIndexpath方法中返回一个cell 您的-tableView:cellForRowAtIndexPath: 方法在哪里实际引发异常? 返回单元格已经存在.. 【参考方案1】:

这里的解决方案是在第二个Tableview控制器中添加条件,如果单元格为nil,则必须显示表格视图样式。在

中添加以下代码

cellForRowAtIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath



static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];





// Configure the cell...

return cell;

【讨论】:

以上是关于从 tableviewcontroller 加载 tableviewcontroller 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

从 TableViewController 中关闭 ViewController

View Controller未显示TableViewController中的数据

倒带时自动重新加载 TableViewController

在 tableviewController 中重新加载数据失败

如果当前视图是 tableViewController,iOS TableView 不会重新加载

在 tableViewController 中使用重新加载的 CoreData