5

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

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
yassassin
  • 3,185
  • 6
  • 28
  • 31

2 Answers2

5

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.

Vincent Bernier
  • 8,674
  • 4
  • 35
  • 40
  • 1
    Incredible, it was in front of me and I didn't see it!! :) As you suggested, it was enough doing the following: `correspondingIndex = [[fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];`. Thank you very much!!! – yassassin Dec 30 '11 at 19:31
2

To support additional languages I wouldn't use hard coded characters. My solution inspired by the accepted answer :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSString *correspondingLetter = [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] objectAtIndex:index];
    return [[self.fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];
}
hatunike
  • 1,967
  • 1
  • 19
  • 26