我似乎遇到了一整天都在努力解决的问题。我有一个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 :(得分:1)
在prepareForSegue
之前调用didSelectItemAtIndexPath
之类的声音。因此,prepareForSegue
会看到上次调用selectedFavoriteIndex
时didSelectItemAtIndexPath
的值。
通过在两种方法中放置NSLog
语句来查看日志消息在控制台中的显示顺序,这非常容易确认。
有几种方法可以解决这个问题:
不是使用故事板定义的segue,而是在使用didDeselectItemAtIndexPath
设置selectedFavoriteIndex
之后,在[self.storyboard instantiateViewControllerWithIdentifier:]
中执行程序设计。
不是依靠didSelectItemAtIndexPath
来设置所选索引,而是通过调用prepareForSegue
来确定[self.collectionView indexPathForCell:sender]
中的所选索引(当您使用集合视图单元格中的故事板segue时) ,sender
中prepareForSegue
的值将是单元格本身,您可以使用它来确定索引路径。
方法(2)看起来像这样:
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
NSManagedObject *selectedFavorite = [self.favorites objectAtIndex:indexPath.item];