I've implemented a table populated with core data and now I'm trying to indexing it with sections by displaying the side Alphabet (in Contacts like format).
In the code below, if I use the commented line, I have only letters for existing sections. But I want the whole alphabet, and so I've changed the returned array:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//return [fetchedResultsController sectionIndexTitles];
indexArray = [NSArray arrayWithObjects: @"{search}", @"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];
return indexArray;
}
All the letters are displayed on the side index. But now I've to implement the method that returns the index of the selected section, and here I've some problems:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
NSString *correspondingLetter = [indexArray objectAtIndex:index];
NSUInteger correspondingIndex = [[fetchedResultsController sections] indexOfObject:correspondingLetter];
NSLog(@"------index:%i\ncorrespondingLetter: %@\ncorrespondingIndex: %i\n", index,correspondingLetter, correspondingIndex);
return correspondingIndex;
}
With the code above, if I use the commented line, I have an error each time I select a letter that doesn't have the corresponding section. So what I'm trying to do, is to retrieve the section index using the letter that has been selected and searching its position into existing sections. But it doesn't work. Do you have any ideas?
Thanks in advance, yassa
You should search in
@property (nonatomic, readonly) NSArray *sectionIndexTitles
of the fetchedResultController.
But, if it's an index that doesn't exist you will receive NSNotFound
and will need to do some logic to return the previous letter index, or the previous, or the previous, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With