gpt4 book ai didi

ios - 从 UICollectionView 的 viewForSupplementaryElementOfKind 获取 NSFetchedResultsController 部分对象

转载 作者:可可西里 更新时间:2023-11-01 05:44:49 26 4
gpt4 key购买 nike

我有一个名为Location 的数据模型。我将它们用作 UICollectionViewController 中的部分标题。每个 location 都可以在这些部分中显示 items。我想在 viewForSupplementaryElementOfKind 中自定义我的部分标题。但我无法弄清楚如何从此方法接收正确的 Location 对象。

我试过类似的东西:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView   viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections] [indexPath.section];
Item *item = sectionInfo.objects[0];
Location *location = item.location;
SectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath];
headerView.label.text = location.name;
[...]

但我不断得到:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM   objectAtIndex:]: index 0 beyond bounds for empty array'

这可能是因为位置可能没有任何项目。关于我应该如何做还有其他想法吗?

最佳答案

您的代码看起来非常正确,并且在我的小型测试应用程序中按预期工作。因此,我假设您在数据源方法中存在一些错误。

这是我用的:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.frc sections] count];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.frc sections][section];
return [sectionInfo numberOfObjects];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellView" forIndexPath:indexPath];
Item *item = [self.frc objectAtIndexPath:indexPath];
cell.name.text = item.name;
return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.frc sections][indexPath.section];
Item *item = sectionInfo.objects[0];
Location *location = item.location;
HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerView.title.text = location.name;
return headerView;
}

抓取结果 Controller 创建如下:

- (NSFetchedResultsController *)frc
{
if (_frc == nil ) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"location.name" ascending:NO];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = @[sort1, sort2];
[fetchRequest setSortDescriptors:sortDescriptors];

_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:@"location.name"
cacheName:nil];
NSError *error;
_frc.delegate = self;
[_frc performFetch:&error];
}
return _frc;
}

备注:带有获取结果 Controller 的 Collection View 的自动更新非常棘手,所以这篇文章

http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview

(带有代码示例)可能很有趣。

关于ios - 从 UICollectionView 的 viewForSupplementaryElementOfKind 获取 NSFetchedResultsController 部分对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19607620/

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