【发布时间】:2014-01-30 03:38:57
【问题描述】:
我似乎遇到了一整天都在努力解决的问题。我有一个 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。谁能给我解释一下?
【问题讨论】:
-
0-2 表示第 0 部分,第 2 项
标签: ios objective-c core-data uicollectionview