【问题标题】:Performing segue with prototype cells [duplicate]使用原型单元执行 segue [重复]
【发布时间】:2012-09-03 15:45:45
【问题描述】:

首先原谅我的英语,因为我是法国人:-)

我是一名 iOS 开发初学者,我尝试在 XCode 4.4 中使用 Master Detail 视图。

我想要什么:

我的主视图包含带有原型单元格的 UITableView。当我单击我的单元格时,我想查看详细信息视图。

这是我的代码:

MasterViewController.m

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

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSDictionary* instrument = [Instruments objectAtIndex:indexPath.row];
    NSLog(@"instrument: %@",instrument);

    NSString* status = [instrument objectForKey:@"status"];
    NSLog(@"status: %@",status);

    NSString* imageName = [NSString stringWithFormat:@"%@48.png", [status lowercaseString]];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    imgView.image = [UIImage imageNamed:imageName];
    cell.imageView.image = imgView.image;

    // Set up the cell...
    NSString *cellValue = [instrument objectForKey:@"id"];
    cell.textLabel.text = cellValue;

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"masterToDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        NSDictionary* instrumentDetails = [Instruments objectAtIndex:indexPath.row];

        [[segue destinationViewController] setDetailItem:instrumentDetails];
    }
}

当然,我的故事板中有一个 segue,将我的原型单元格链接到详细视图,其中标识符为“masterToDetails”。

当我单击主表中的原型单元格时,不会调用 prepareForSegue。 为什么?

然后当我尝试强制 segue 调用时:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"masterToDetails" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}

我有以下错误:NSInvalidArgumentException,原因:Receiver has no segue with identifier masterToDetails

但它存在于我的故事板中!

也许我使用 MasterDetail 的方式是错误的,或者是我的一个愚蠢的错误......

如果您想从我的申请中获得其他详细信息,请告诉我。

【问题讨论】:

    标签: ios5 uitableview segue xcode4.4


    【解决方案1】:

    不要将单元格中的 segue 链接到下一个 viewController,而是从 viewController(View 下方的小橙色图标)拖动到下一个 viewController。 然后确保给 segue 一个标识符,然后使用:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"masterToDetails" sender:indexPath];
    }
    

    (复制并粘贴您的 segue 标识符以减少任何拼写错误)。

    【讨论】:

    • 另外,检查情节提要中单元格的重用标识符。根据您的代码,它应该是“Cell”
    • FWIW,这个简洁但详细的答案比 OP 顶部过于冗长的重复链接价值 100 倍 - ty
    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多