2

私はコアデータを初めて使用し、現在コアデータモデルを使用してアプリを構築しています。私が達成したいのは、各月のセクション ヘッダーで分割されたテーブルビューです。現時点では、各セルにすべてのデータを表示するアプリケーションが動作していますが、各月のセクション ヘッダーはありません。私が欲しいのは、"date" : "26/08/2012" のオブジェクトが、8 月というタイトルのセクションの下に表示されることです。

次のデータを持つjson Webサービスがあります。

 "date": "26/08/2012",
    "hour": "18u00",
    "type": "JPL",
    "home": "KRC Genk",
    "away": "Zulte-Waregem",
    "homeScore": "2",
    "awayScore": "0"

その NSManagedObject サブクラスと、データベース内に値を格納するためのこのクラスのカテゴリを構築します。これはすべて機能します。これらの値をテーブルビューに入れるには、次の方法があります。

- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.genkDatabase.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

}

そして、これは私の cellForRowAtIndexPath で行うことです

static NSString *simpleTableIdentifier = @"KalenderCell";

        KalenderCell *cell = (KalenderCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if ((cell == nil) || (![cell isKindOfClass:KalenderCell.class]))
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KalenderCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        // ask NSFetchedResultsController for the NSMO at the row in question
        Kalender *kalender = [self.fetchedResultsController objectAtIndexPath:indexPath];
        // Then configure the cell using it ...

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM"];
       NSString *dateStr = [dateFormat stringFromDate:kalender.date];
        cell.lblType.text           = kalender.type;
        cell.lblHome.text           = kalender.home;
        cell.lblHomeScore.text      = kalender.homeScore;
        cell.lblAwayScore.text      = kalender.awayScore;
        cell.lblAway.text           = kalender.away;
        cell.lblDate.text           = dateStr;
        cell.lblHour.text           = kalender.hour;


        return cell;

しかし、ここで私は立ち往生しています。NSFetchedResult コントローラーを使用してこれらのセクション ヘッダーを設定する方法を教えてください。

よろしくお願いします!

4

1 に答える 1

0

次のように、NSFetchedResultsController イニシャルのsectionNameKeyPathで使用するフィールドの名前を設定するだけです。

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.genkDatabase.managedObjectContext sectionNameKeyPath:@"date" cacheName:nil];

これにより、日付文字列がセクション名として設定されます。お好きなフィールドをご自由にお使いください。

于 2012-10-08T22:21:44.997 に答える