1

I have an IOS app that is using RestKit to pull json formatted data from a server into a CoreData Entity. Some of the attributes of the entity help populate a collection view with an image and title for each cell.

I am attempting to move from the collection view to a detail view when a cell is selected. There is a "summary" attribute of the CoreData Entity that I would like to display in the detail view along with the image.

I know I can pass data thru the prepareForSegue method. But I am not sure how to specify the image and summary data I want to pass.

Maybe passing the image and summary is not the proper way? Should I be passing the managedObjectContext to the detail view controller and fetching the results from there?

Here is how my CollectionView is populated.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

MyNewsCollectionViewCell *cell = (MyNewsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:@"imageUrl"]];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];        
[cell.title setText:[object valueForKey:@"title"]];      

return cell;

Here is the prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"newsDetail"]) {

      NewsDetailViewController *vc =[segue destinationViewController];
      vc.newsDetailImageView = //How do I designate the image from the cell I selected???
      vc.newsDetailText = //How do I designate the text from the cell I selected???             

    }
}

This is obviously a beginners question.... any help would be much appreciated. Given that I'm a beginner basic example code really helps!

Ben
  • 967
  • 3
  • 9
  • 23

2 Answers2

0

If the cell selection triggers the segue instantly, you can make use of the indexPathForSelectedItems method of UICollectionView to get the indexPath that you need to get your NSManagedObject.

Try this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"newsDetail"]) {
        NewsDetailViewController *vc =[segue destinationViewController];
        // In the following line, replace self.collectionView with your UICollectionView
        NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSURL *photoURL = [NSURL URLWithString:(NSString *)[object valueForKey:@"imageUrl"]];
        NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
        UIImage *img = [[UIImage alloc] initWithData:photoData];

        vc.newsDetailImageView = [[UIImageView alloc] initWithImage:img];
        vc.newsDetailText = howeverYouGetYourObjectSummary;            
    }
}
Simon M
  • 2,931
  • 2
  • 22
  • 37
  • Thanks Simon! This works! I also found the code that comes in the prepareForSegue method of the Master-Detail template works as well. I don't know why I didn't try it before I asked the question! I appreciate the your answer more because now I under stand the indexPathForSelectedItems method! Thanks again! – Ben May 21 '13 at 04:09
0

You could add a property to you MyNewsCollectionViewCell, like so:

@property (weak,nonatomic) NSManagedObject* myObject;

Then you could assign this property the NSManagedObject for this cell in cellForItemAtIndexPath.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        /* Add this line to All that u r already doing*/   
        [cell setMyObject:object];
        return cell;
    }

Now in didSelectCellMethod you can call [self performSegueWithIdentifier: @"MySegue" sender: cell];

and then in prepareForSegue: get the object from the sender.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MyNewsCollectionViewCell)sender
            {
                if ([[segue identifier] isEqualToString:@"newsDetail"]) {
                    NewsDetailViewController *vc =[segue destinationViewController];
                    NSManagedObject* object = sender.myObject;

                    //use this object to set values of 

                    vc.newsDetailImageView = /*set value*/
                    vc.newsDetailText = /*set value*/            
                }
        }
Simon M
  • 2,931
  • 2
  • 22
  • 37
ila
  • 920
  • 12
  • 35
  • Ila, thanks for the response. I was able to get my code working on Simons answer first but I believe looking over yours that I could do it this way as well. I haven't proved that out but it seems logical and a solid solution to the question. Thanks! – Ben May 21 '13 at 04:11