Reputation: 1458
I have a pretty standard UITableView, with sections (A-Z - no numbers or search).
I've implemented the delegate methods that enable the section index (A-Z bar) view on the right side of the table.
I've now inputted enough data into the table such that most (not all but most) of the sections (i.e. A, B, C...Z) have one or several cells.
However I've noticed, now that the app is in private beta that when someone touches a letter of the section index, the actual position the table jumps to is incorrect.
It's actually correct for A, B and C and then incrementally goes more and more wrong - such that Z is actually where 'R' is on the bar.
This is on iPad (only) and no special 'behind the scenes' customisation is going on for the table or section index view.
Any ideas? I've been googling for almost an hour and can't find anyone that's seen this before.
EDIT:
Here is my code for setting up the Section Index:
- ( NSArray * ) sectionIndexTitlesForTableView:( UITableView * )tableView
{
[self customizeSectionIndexView]; // This just changes colours in iOS 7+
return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
}
- ( NSString * ) tableView:( UITableView * )tableView titleForHeaderInSection:( NSInteger )section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsControllerWithPredicate sections] objectAtIndex:section];
return [sectionInfo name];
}
- ( NSInteger ) numberOfSectionsInTableView:( UITableView * )tableView
{
NSInteger sectionCount = [[self.fetchedResultsControllerWithPredicate sections] count];
return sectionCount;
}
- ( NSInteger ) tableView:( UITableView * )tableView numberOfRowsInSection:( NSInteger )section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsControllerWithPredicate sections][section];
return [sectionInfo numberOfObjects];
}
So I have at most 26 sections, but I'm only trying to display those that have cells within them. Is this where I'm going wrong?
How do I handle this state then when the data is coming in from Core Data and can change at any time?
Upvotes: 0
Views: 1003
Reputation: 4140
You need to make sure you have as many sections as you do indexes, for example if you are doing A..Z you need 26 indexes ("A", "B", "C" etc) as well as 26 table sections.
If you display the table section header as a letter as well and you don't want the section header to appear if there are no cells in that section, you can return nil for that section.
This is how I do it:
-(NSArray*) letters {
return @[@"#", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}
-(NSArray*) sectionIndexTitlesForTableView:(UITableView*)tableView {
return [self letters];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView {
return [self letters].count;
}
-(NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
// Get array of items in this section
NSArray* sectionItems = [allSections objectAtIndex:section];
// Return letter, or nil if no items in this section
return (sectionItems.count > 0 ? [[self letters] objectAtIndex:section] : nil);
}
Upvotes: 2