didSelectedRowAtIndexPath - prepareForSegue - Storyboard

user1256827 picture user1256827 · Mar 13, 2012 · Viewed 8.2k times · Source

i'm trying to send data to my DetailViewController according to the row i selected in my TableView. My project is made with a StoryBoard.

My TableViewCell is well linked to my UIView in my MainStoryBoard, everything is fine expect that i don't know how to send the information of the row selected to my DetailView

Here is what i have:

- (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 

    }

}

Answer

Francesco picture Francesco · Mar 13, 2012

When you call "performSegueWithIdentifier:sender:" instead of passing self to sender, pass the information needed later (that is, either indexPath, or the object of your model represented by indexPath) Later in prepareForSegue (inside your if) you can cast the sender parameter to the object you passed earlier, and you can obtain the destination view controller with [segue destinationViewController]; At this point you can set on your destination view controller some properties (like images, etc)..

So...

- (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 = ....
}

EDIT: Your "DetailGrandComptes" class should have the properties or ivars you need! So.. if you need a label and an image, your class should be like this:

@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