我使用xcode 4.2和Storyboard创建了一个iOS选项卡式应用程序。我添加了一个带有自定义单元格的tableviewcontroller,单击该行时,我想打开一个tableviewcontroller,我在下面使用了以下代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
categoryClass *cc = [datas objectAtIndex:indexPath.row];

[tableView deselectRowAtIndexPath:indexPath animated:NO];

iSubProducts *subProducts = [[iSubProducts alloc] init];
subProducts.title = cc.categoryName;
subProducts.catID = cc.categoryID;
[[self navigationController] pushViewController:subProducts animated:YES];
[subProducts release];


}

但是当我单击该行时,出现以下错误:

*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格

在我的iSubProducts tableviewcontroller上,我具有以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCell2";

iSubProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

productSubClass *cc = [datas2 objectAtIndex:indexPath.row];
NSLog(@"Product Name: %@", cc.productName);
cell.txtProductName.text  = cc.productName;
cell.txtProductDesc.text = cc.productDesc;

return cell;

}

我假设这是发生错误的地方,单元格返回的是nil值。当我尝试使用或通过按钮附加iSubProducts tableviewcontroller时,一切正常,但是如果单击它来自行,则会显示此错误。

我是iOS开发的新手,也许从带有自定义单元格的tableviewcontroller打开tableviewcontroller时出错。我已经动摇了两天了,谷歌搜索了很多,不幸的是我没有找到任何解决方案。我很确定iSubProducts tableviewcontroller上没有错误,因为如果我尝试从按钮上按下它,它将起作用。请在这方面我需要建议,我现在就陷在这个问题上了。谢谢大家。

最佳答案

请注意,这肯定有多大好处。.但是直到今天,单击行后,我还是无法从一个表视图移动到另一视图。

我完全远离DidSelectRowAtIndexPath。相反,我使用了segue(这就是我的目标)。

这段代码摘自苹果使用表的情节提要示例。

http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Listings/Classes_RootViewController_m.html#//apple_ref/doc/uid/DTS40007416-Classes_RootViewController_m-DontLinkElementID_10

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ShowSelectedPlay"]) {
            NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
            DetailViewController *detailViewController = [segue destinationViewController];
            detailViewController.play = [dataController objectInListAtIndex:selectedRowIndex.row];
    }
}

所有这些假定使您开始在情节提要板功能中使用序列,并确保使用序列的标识符。

10-07 19:54
查看更多