【问题标题】:xcode 4.2 iOS Tableview using storyboardxcode 4.2 iOS Tableview 使用情节提要
【发布时间】:2011-12-13 19:50:53
【问题描述】:

我使用 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 数据源必须从 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 时出错。我现在已经头疼了 2 天了,用谷歌搜索了很多,不幸的是我没有找到任何解决方案。我很确定 iSubProducts tableviewcontroller 没有错误,因为如果我尝试从按钮按下它,它就可以工作。请我需要关于这个的建议,我现在很纠结这个问题。谢谢大家。

【问题讨论】:

    标签: ios tableview storyboard


    【解决方案1】:

    请注意这是多么有益。但就在今天,我在单击该行后无法从一个表格视图移动到另一个视图。

    我完全远离 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];
        }
    }
    

    这一切都假设您开始在情节提要功能中使用转场,并确保使用转场的标识符。

    【讨论】:

    • 我认为这是正确的——上面代码中的问题是新的表格视图控制器是直接分配/初始化的,而不是从情节提要中加载的,这意味着原型单元格不可用。使用 segue 并在 prepareForSegue 中配置目标视图控制器将解决这个问题,或者它可以从情节提要中实例化,但这似乎是不必要的代码。
    • 哇,这解决了问题,谢谢!我只能说我的上帝!再次感谢您。
    【解决方案2】:

    您只需要先在设计器中创建一个原型单元格,这样您就可以从单元格中拖出一个segue 到目标视图。

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 2012-02-19
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 2012-05-18
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多