didSelectedRowAtIndexPath - prepareForSegue - Storyboard

时间:2012-03-13 16:09:31

标签: objective-c storyboard tableview detailview

我正在尝试根据我在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 

    }

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

MyViewController *destination = [[segue destinationViewController] topViewController];