gpt4 book ai didi

ios - UICollectionView 始终在当前之前显示上一个选择

转载 作者:行者123 更新时间:2023-11-29 03:17:59 24 4
gpt4 key购买 nike

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

{
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。谁能给我解释一下?

最佳答案

听起来像 prepareForSeguedidSelectItemAtIndexPath 之前被调用。因此,prepareForSegue 从上一次调用 didSelectItemAtIndexPath 中看到了 selectedFavoriteIndex 的值。

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

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

  1. 不是使用 Storyboard定义的转场,而是在 didDeselectItemAtIndexPath 设置 selectedFavoriteIndex 中使用 [ self.storyboard instantiateViewControllerWithIdentifier:].

  2. 不是依靠 didSelectItemAtIndexPath 来设置选定的索引,而是通过调用 [self.collectionView indexPathForCell:sender] 在 prepareForSegue 中确定选定的索引(当您从 Collection View 单元格使用 Storyboard segue 时,prepareForSegue 中的 sender 的值将是单元格本身,您可以使用它来确定索引路径)。

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

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

关于ios - UICollectionView 始终在当前之前显示上一个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com