开发者

Anyone know how to make the table index the first 4 chars of my core data name attribute?

开发者 https://www.devze.com 2023-03-17 11:44 出处:网络
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

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 @"titleForSecti开发者_StackOverflow社区on" 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!


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:


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消