Reputation:
I seem to be having a problem I have been trying to solve all day. I have a UICollectionView. Within this view, items from core data are shown(images). You click on the image and it goes to a detail view with more information that was inputted when the image was taken via the camera. The images show fine in the collection view. The problem I am having is that always before showing the current selection, the last selection is shown first. I don't know what to do, here is the code and some output.
{
NSInteger selectedFavoriteIndex;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication]delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//Fetch all the stuff from data store.
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]initWithEntityName:@"Favorites"];
self.favorites = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];
[self.collectionView reloadData];
[self.collectionViewLayout invalidateLayout];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return [self.favorites count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCollectionCell *customCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *favorite = [self.favorites objectAtIndex:indexPath.row];
[customCell.collectionImageView setImage:[UIImage imageWithData:[favorite valueForKey:@"image"]]];
return customCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedFavoriteIndex = indexPath.item;
NSLog(@"Index path: %@", indexPath);
NSLog(@"Selected Favorite Index: %ld", (long)selectedFavoriteIndex);
NSLog(@"IndexPath.row: %ld", (long)indexPath.row);
NSLog(@"IndexPath.item: %ld", (long)indexPath.item);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:@"moment"]) {
NSManagedObject *selectedFavorite = [self.favorites objectAtIndex:selectedFavoriteIndex];
ViewMomentViewController *viewMomentViewController = segue.destinationViewController;
viewMomentViewController.favorite = selectedFavorite;
viewMomentViewController.hidesBottomBarWhenPushed = YES;
}
}
The formatting kind of got messed up when pasting it here. The format is correct in Xcode though. Here is some output.
Index path: {length = 2, path = 0 - 1}
I am also confused as why the path is always subtracting, whether it be 0-0 or 0-2. Can someone explain that to me?
Upvotes: 1
Views: 1204
Reputation: 9915
Sounds like prepareForSegue
gets called before didSelectItemAtIndexPath
. So prepareForSegue
sees the value of selectedFavoriteIndex
from the previous call to didSelectItemAtIndexPath
.
This is really easy to confirm by placing NSLog
statements in both methods to see the order in which the log message appear in the console.
There are a couple of ways to solve this:
Rather than using a storyboard-defined segue, do a programatic segue in didDeselectItemAtIndexPath
after setting selectedFavoriteIndex
using [self.storyboard instantiateViewControllerWithIdentifier:]
.
Instead of relying on didSelectItemAtIndexPath
to set the selected index, determine the selected index in prepareForSegue
by calling [self.collectionView indexPathForCell:sender]
(when you use a storyboard segue from a collection view cell, the value of sender
in prepareForSegue
will be the cell itself, which you can use to determine the index path).
Approach (2) would look something like this:
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
NSManagedObject *selectedFavorite = [self.favorites objectAtIndex:indexPath.item];
Upvotes: 1