UITableView部分用一个属性排序,并为标题显示另一个属性

时间:2012-06-09 14:59:52

标签: uitableview ios4 ios5

我有一个核心数据UITableView,核心数据管理对象有4个属性(headerTitle,headerNumber,objectTitle,objectComment)。

我希望由headerNumber划分的UITableView,但是titleForHeaderInSection是headerTitle属性。目前我在下面有这个代码......

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[__fetchedResultsController sections] objectAtIndex:section];
    return [NSString stringWithFormat:@"%@", [sectionInfo name]];
}

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects: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:self.managedObjectContext sectionNameKeyPath:@"headerNumber" cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
} 

headerNumber是2012年6月的201206年的数字年份和月份。 headerTitle是headerNumber的单词,例如“June 2012”

有没有人有任何建议?

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法在表视图中显示headerTitle属性

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];

//return a view with uiLable and show your headerTitle there

}

并使用以下方法显示headerNumber属性

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
      // put your code here 
}

跳这会帮助你

相关问题