Storyboarding tip for passing Core Data objects accurately from a table view

As some of you may have been following along with Apple’s Storyboarding (an enhancement to Interface Builder), and the means of being able to transition from scene to scene with the new function prepareForSeque……

Big Picture:   I have a table view, and in each cell, I have an invisible button sitting on top of an image which is to launch a new view, so I can essentially view the image larger, and zoom, pinch, move around (in a scroll view), etc.   In the old world one would have  tableview methods for touch activities.In the past I could leverage tableView didSelectRowAtIndexPath:,  I could easily grab the row index, and go about my business….

Pitfall:  I have and found a little pitfall in passing a core data object from a tableview with accuracy.  I would get only the top object from my tableview row regardless of what row I chose… This equates to “not shippable”, or simply a bug in my code.

Solution: Back to the Storyboard and seque constructs, I’m utilizing the prepareForSeque to transition and essentially attempting to write less code and pass the appropriate MOC (managed object context) for the current table cell row.   Bear in mind, I am using the rest of the cell to go to a full detailed view, and not the image view to zoom, so this is a separate avenue navigation wise.

I discovered from browsing the web that the use of Super would come in handy, or in this case Superview

I basically searched the web, and found a post on stack overflow, and it reference an old book of mine, and boom, there it was with a small snippet of code to solve my dilemma.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 if ([[segue identifier] isEqualToString:@"zoom"])
 {
 // Set up Destination View Controller
 ImageZoomViewController *imgc = (ImageZoomViewController *)[segue destinationViewController];
 // Pass the destination view the MOC
 imgc.managedObjectContext = managedObjectContext;
// This is a bit below is esoteric, but what I'm doing here is getting a reference to the superview of the button (cell)
 // This won't normally work in a context of Storyboards without the old school didSelectRowAtIndexPath route
 // However because I'm using the story board construct, this works nicely. for passing in correct data object
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
 NSUInteger buttonRow = [[self.tableView indexPathForCell:clickedCell] row];
 imgc.currentPhoto = [wishListData objectAtIndex:buttonRow];
 }

}

Please excuse this ugly format above.  I’m looking for better ways to present code in the WP free universe….

Credit where credit is due:

StackOverFlow link

Beginning iPhone 3 Development book link

Advertisements