有人知道如何使表索引成为我的核心数据名称属性的前4个字符吗



我正在开发一个硬币应用程序。硬币在由核心数据管理的表视图中呈现给用户。

所有的硬币名称都以"19"或"20"开头。当我在表视图上实现节索引时,我在索引中只得到一个"1"和一个"2"。按"1"键将表格移到"1900"硬币,按"2"键将我移到"2000"硬币。我知道为什么,它来自名称字段的第一个数字。

我想要的是"1910"、"1920"、"1930"等,这样用户就可以跳到十年。

我在模型中添加了一个名为"titleForSection"的属性,并输入了"1910"、"1920"等,并在获取请求中将sectionNameKeyPath设置为我的@"titleForSection"属性。不用说,它不起作用。

有人知道如何使节索引为name属性的前4位吗?

- (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;
}

更新:

我将"titleForSection"属性从字符串更改为数字,然后从1900年到2010年,按十年填充数据库。现在,我的表索引只显示为"0"、"1"one_answers"2"。我只是不明白为什么我不能在里面放一个数字!

您应该覆盖UITableViewController中的– sectionIndexTitlesForTableView:,尝试如下操作:

- (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;
}

你可能感兴趣的第二种方法:

– tableView:sectionForSectionIndexTitle:atIndex:

只需为您的coin类创建一个新方法:

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

然后对NSFetchedResultsController init 中的"sectionNameKeyPath"使用该方法

NSFetchedResultsController *aFetchedResultsController = 
     [[NSFetchedResultsController alloc] 
           initWithFetchRequest:fetchRequest 
           managedObjectContext:managedObjectContext 
             sectionNameKeyPath:@"firstFourCharsOfTitle" 
                      cacheName:nil];

最新更新