3

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

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

我想要的是:

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

这是我的代码:

主视图控制器.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 的方式是错误的,或者这可能是我的一个愚蠢的错误......

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

4

1 回答 1

8

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

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

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

于 2012-09-03T20:40:31.163 回答