2

I'm working on a coin app. Coins are presented to the user in a tableview, managed by Core Data.

All the coin names begin with either "19" or "20". When I implement a section index on the table view, I only get a "1" and a "2" in my index. Pressing the "1" moves the table to the "1900" coin, and pressing the "2" leads me to the "2000" coin. I know why that is, it's coming from the first digit in the name field.

What I'd like is "1910", "1920", "1930", etc, so the user can jump to the decade.

I added an attribute called "titleForSection" to the model and entered "1910", "1920", etc and figured in my fetch request set sectionNameKeyPath to my @"titleForSection" attribute. Needless to say, it doesn't work.

Anyone know how to make the section index the first 4 digits of the name attribute?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [[fetchedResultsController sections] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.searchIsActive) {
        return [self.filteredListContent count];
    }

    NSInteger numberOfRows = 0;

    if ([[fetchedResultsController sections] count] > 0) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
        numberOfRows = [sectionInfo numberOfObjects];
    }

    return numberOfRows;

}



//for index
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return [fetchedResultsController sectionIndexTitles];

}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;

    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Coins" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    //set batch size
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"titleForSection" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}

UPDATE:

I changed my "titleForSection" attribute from a string to a number and then populated the database from 1900 all the way to 2010, by decade. Now my table index only appears with "0", "1", and "2". I just don't understand why I can't place a number in there!

RyeMAC3
  • 1,023
  • 1
  • 10
  • 17

2 Answers2

0

You should override – sectionIndexTitlesForTableView: in UITableViewController, try something like this:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:@"1920"];
    [array addObject:@"1930"];
    [array addObject:@"1940"];
    [array addObject:@"1950"];
    [array addObject:@"1960"];
    [array addObject:@"1970"];

    return array;
}

Second method you can be interested in:

– tableView:sectionForSectionIndexTitle:atIndex:
alhcr
  • 823
  • 4
  • 12
  • 21
  • Yeh, but how does the table know to jump to 1940 when its index is pressed? What makes them "line up"? – RyeMAC3 Jul 05 '11 at 01:05
  • First object in this array jump to first row, second to second, third to third, etc. so if this array is too small you wont be able to jump to last rows. – alhcr Jul 05 '11 at 01:26
  • Yes, and this table has 350 records. So I don't want to do it that way. Also, is the user deletes a row, then this goes out the window. – RyeMAC3 Jul 05 '11 at 02:46
  • what's the problem? just generate this array and change it if user inserts/removes rows – alhcr Jul 05 '11 at 03:13
0

Just create a new methode to your coin class:

-(NSString*)firstFourCharsOfTitle
{
    return [[self titleForSection] substringToIndex:4];
}

and than use that methode for the "sectionNameKeyPath" in the NSFetchedResultsController init

NSFetchedResultsController *aFetchedResultsController = 
     [[NSFetchedResultsController alloc] 
           initWithFetchRequest:fetchRequest 
           managedObjectContext:managedObjectContext 
             sectionNameKeyPath:@"firstFourCharsOfTitle" 
                      cacheName:nil];
Reinhard
  • 1,516
  • 1
  • 18
  • 25