0

私はコア データ UITableView を持っています。コア データ管理オブジェクトには 4 つのプロパティ (headerTitle、headerNumber、objectTitle、objectComment) があります。

UITableView を headerNumber で区分したいのですが、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」です。

誰か提案はありますか?

4

1 に答える 1

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 
}

これはあなたを助けるでしょう

于 2012-06-14T10:11:19.137 に答える