there’s an interesting discussion on Apple Development forum about computing table view sections manually for large row set. For viewing it a developer account is required:
NSFetchedResultsController fetching all objects in the DB…
To reassume for those without a dev account, an Apple technician suggest to use an entity containing index title, with to-many relationship to entities you want to display in rows.
The typical example is a collection of song or artist, where the index section title is the first letter A,B,C…
So, entity with title A will have a to-many relationship with songs starting with letter A, and so on.
The mechanism is to use a fetched results controller to retrieve all the song, and at the same time, launch a fetch request for retrieving an NSArray of indexes.
NSFetchRequest *req = //fetch request for section entity
NSArray *sections = [MOC executeFetchRequest:req error:&error];
It is quite easy to get sections count and rows in sections:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
NSInteger abc = [self.sections count];
return abc;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CardSection *s = (CardSection*)[self.sections objectAtIndex:section];
NSInteger rows = [s.cards count];
return rows;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
CardSection *s = [self.sections objectAtIndex:section];
NSString *title = s.title;
return title;
}
However, the problem starts in cell for row at index path:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject obj = [_fetchedResultsController objectAtIndexPath:indexPath];
// build cell....
return cell;
}
Because obviously the index path is referred to computed sections and rows, and therefore fetched controller goes out of range.
Of course this could be solved by recalling the section entity and ask for a specific index object in NSSet relationship, but in this way the benefit of having a fetched controller are lost.
I wonder if someone has tried this approach, and how did he manage to solve the issue.
The only solution I found so far, is to post parse the sections array to find the amount of preceding object at a specific index.
For example supposing this is my sections where [a,b] are just NSIndexPath:
if I am at index 2, the [self.totalAtIndex objectAtIndex:2] would contain the amount of object stored before at index 0 + index 1, therefore return 3.
And the index [2,1] is converted into [0,5].
And this is the correspondent cellForRow:atIndexPath:
If someone has a better solution…