I am new to core data and NSFetched resultcontroller. So far I managed to fill up my tableView. But now I want to divided into sections. Here is how my code looks like.
- (void)getKeepers // attaches an NSFetchRequest to this UITableViewController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Team"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"sortOrder" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.genkDatabase.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}
Let me sketch the situation. I am making an app for a football club. In my tableview I want for every position (goalkeeper,defender,winger,attacker) a new section. My core database looks like this.
- TEAM
-name
-Position
-img_url
-birthDate
-sortOrder
I added the attribute sortOrder to sort my players. But can anybody help me to divide it into sections?
Thanks in advance !!
WHAT I DO IN MY CELL_FOR_ROW_AT_INDEX
In my cellForRowAtIndex I am doing not the usual thing. I am working with a custom tableviewCell which contains 6 imageviews. But its possible that a row only contains 4 images. You can see what I am trying to do over here.
#define IMAGES_PER_ROW 6
NSInteger frcRow = indexPath.row * IMAGES_PER_ROW; // row in fetched results controller
for (int col = 1; col <= IMAGES_PER_ROW; col++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:frcRow inSection:0];
Team *team = [self.fetchedResultsController objectAtIndexPath:path];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
UIImage *image;
if (imgData == nil) {
// default image
image = [UIImage imageWithContentsOfFile:@"keeperNil.jpg"];
} else {
image = [UIImage imageWithData:imgData];
}
[cell setImage:image forPosition:col];
frcRow ++;
}
Use the
sectionNameKeyPathvalue to be the field you want to use for the sections.Then you need these three functions…
This should be enough.
Your cellForRowAtIndexPath function should look something like this…
Or something like that.
The error you are getting means you are trying to access an array that doesn’t contain enough entries.
If that doesn’t work then post your cellForRowAtIndexPath function.