UICollectionView始终显示当前的先前选择

时间:2014-01-30 03:38:58

标签: ios objective-c core-data uicollectionview

我似乎遇到了一整天都在努力解决的问题。我有一个UICollectionView。在该视图中,显示来自核心数据的项目(图像)。单击图像,它将转到详细视图,其中包含通过相机拍摄图像时输入的更多信息。图像在集合视图中显示正常。我遇到的问题是,在显示当前选择之前,首先显示最后一个选择。我不知道该怎么做,这里是代码和一些输出。

{
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;



}


}

在这里粘贴格式时,格式化了。但是在Xcode中格式是正确的。这是一些输出。

索引路径:{length = 2,path = 0 - 1}

我也很困惑,为什么路径总是减去,无论是0-0还是0-2。有人可以向我解释一下吗?

1 个答案:

答案 0 :(得分:1)

prepareForSegue之前调用didSelectItemAtIndexPath之类的声音。因此,prepareForSegue会看到上次调用selectedFavoriteIndexdidSelectItemAtIndexPath的值。

通过在两种方法中放置NSLog语句来查看日志消息在控制台中的显示顺序,这非常容易确认。

有几种方法可以解决这个问题:

  1. 不是使用故事板定义的segue,而是在使用didDeselectItemAtIndexPath 设置selectedFavoriteIndex之后,在[self.storyboard instantiateViewControllerWithIdentifier:] 中执行程序设计。

  2. 不是依靠didSelectItemAtIndexPath来设置所选索引,而是通过调用prepareForSegue来确定[self.collectionView indexPathForCell:sender]中的所选索引(当您使用集合视图单元格中的故事板segue时) ,senderprepareForSegue的值将是单元格本身,您可以使用它来确定索引路径。

  3. 方法(2)看起来像这样:

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
    NSManagedObject *selectedFavorite = [self.favorites objectAtIndex:indexPath.item];