2

我正在尝试根据我在 TableView 中选择的行将数据发送到我的 DetailViewController。我的项目是用 StoryBoard 制作的。

我的 TableViewCell 很好地链接到我的 MainStoryBoard 中的 UIView,一切都很好,但我不知道如何将所选行的信息发送到我的 DetailView

这是我所拥有的:

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

{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSUInteger oldRow = [lastIndexPath row];
    NSString *key = [keys objectAtIndex:section];
    detailRow = [names objectForKey:key];
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
    if (cell == nil)
    {
        CGRect cellFrame = C

GRectMake(0, 0, 300, 65);
        cell = [[UITableViewCell alloc] initWithFrame:cellFrame] ;
    }
    CGRect nameLabelRect = CGRectMake(200, 5, 200, 15);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
    nameLabel.tag = kNameValueTag;
    [cell.contentView addSubview: nameLabel];

    UILabel *name  = (UILabel *)[cell.contentView viewWithTag:kNameValueTag];
    name.text = [[detailRow objectAtIndex:row] valueForKey:@"name"];

    cell.imageView.image = [UIImage imageNamed:[[detailRow objectAtIndex:row] valueForKey:@"image"]];

    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
        UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return cell;
}



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

    [self performSegueWithIdentifier:@"DetailGrandComptes" sender:self];
}



    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

    if ([[segue identifier] isEqualToString:@"DetailGrandComptes"]) 
    {

      // HERE IS WHERE THE CODE SHOULD BE I GUESS.... !! I HAVE TO DISPLAY AN UIIMAGEVIEW + UILABEL + UIIMAGEVIEW 

    }

}
4

2 回答 2

9

当您调用“performSegueWithIdentifier:sender:”而不是将 self 传递给发件人时,传递稍后需要的信息(即 indexPath 或 indexPath 表示的模型对象)稍后在 prepareForSegue 中(在您的 if 中)您可以转换sender 参数到您之前传递的对象,您可以使用 [segue destinationViewController] 获取目标视图控制器;此时,您可以在目标视图控制器上设置一些属性(如图像等)。

所以...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"DetailGrandComptes" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"DetailGrandComptes"]) 
{
   MyViewController *destination = [segue destinationViewController];
   NSIndexPath * indexPath = (NSIndexPath*)sender;
   destination.imageName = [[detailRow objectAtIndex:indexPath.row] valueForKey:@"image"]
  destination.nameString = ....
}

编辑:您的“DetailGrandComptes”类应该具有您需要的属性或 ivars!所以..如果你需要一个标签和一个图像,你的类应该是这样的:

@interface DetailGrandComptes : UIViewController

@property (strong, nonatomic) NSString *imageName;
@property (strong, nonatomic) NSString *nameString;

@end

//in you .m
@interface DetailGrandComptes()
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *name;
@end

@implementation DetailGrandComptes

- (void)viewWillAppear
{
    [super viewWillAppear];
    name.text = nameString;
    image.image = [UIImage imageNamed:imageName];
}

@end
于 2012-03-13T16:45:32.153 回答
0

此外,如果目标视图控制器是导航视图控制器。应该:

MyViewController *destination = [[segue destinationViewController] topViewController];
于 2013-04-13T14:21:56.287 回答