I have a data model called Location
. I use these as my section headers in a UICollectionViewController
. Each location
can display items
inside these sections. I want to customise my section headers in viewForSupplementaryElementOfKind
. But I can't figure out how to recive the correct Location
objects from this method.
I have tried things like:
-(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;
[...]
But I keep getting:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
This is probably because it's possible to have Location without any items. Any other ideas on how I should do it?
Your code looks quite correct, and it worked as expected in my small test app. Therefore I assume that you have some error in the data source methods.
This is what I used:
The fetched results controller is created as follows:
Remark: The automatic updates of a collection view with a fetched results controller are quite tricky, so this article
http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview
(with code examples) might be interesting.