【发布时间】:2013-10-26 14:03:34
【问题描述】:
我有一个名为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'
这可能是因为位置可能没有任何项目。关于我应该如何做的任何其他想法?
【问题讨论】:
-
问题是否仅出现在没有项目的部分?换句话说,如果所有部分都不为空,您是否获得了正确的 Location 对象?
-
不,但是如果它没有任何物品并且我不能
item.location,我不确定如何获得正确的位置。 -
实际上,我想知道当您使用上一个问题的答案中的设置时,您如何获得零项的部分。您的数据源或委托方法可能存在问题。但我不确定......
-
@MartinR 是的,你是对的。再次尝试使用断点,它已经在
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][indexPath.section];上崩溃。你认为我应该使用另一种方法吗?
标签: ios objective-c cocoa-touch uicollectionview nsfetchedresultscontroller