1

ここに私のデータモデルがあります: データ・モデル

データベースにデータを入力しました。

これをテーブルビューに表示したいと思います。まず、「日」属性を持つ日を選択してから、そのレッスンを取得します。ヘッダーに属性「end」と「start」を表示したい ( -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section)

次に、右側のセクションにレッスン データを表示します。NSFetchedResultsController を使用してそれを行うにはどうすればよいですか?

私は基本的なコードを設定しました:

NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Lessons class])];

NSSortDescriptor* sortGroup = [NSSortDescriptor sortDescriptorWithKey:@"lesson" ascending:YES];
fetch.sortDescriptors = @[sortGroup];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

NSError* error;
[controller performFetch:&error];
if (error) {
    NSLog(@"Error: %@", error);
    abort();
}

編集:

ストーリーボードは次のとおりです。

絵コンテ

アップデート

これが私のコードです:

TableView.m (重要なメソッド)

-(id)init
{
    self = [super init];
    if (self) {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        self.del = [[UIApplication sharedApplication] delegate];
        self.managedObjectContext = self.del.managedObjectContext;

        NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Lessons"];

        NSString *theSelectedDay = @"Mi";

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"lessonToDay.day == %@", theSelectedDay];
        fetch.predicate = pred;
        NSSortDescriptor *sortTime = [NSSortDescriptor sortDescriptorWithKey:@"lessonToTime.start" ascending:YES];
        //NSSortDescriptor *sortGroup = [NSSortDescriptor sortDescriptorWithKey:@"lesson" ascending:YES];
        fetch.sortDescriptors = @[sortTime];

        self.controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch
                                                              managedObjectContext:self.managedObjectContext
                                                                sectionNameKeyPath:@"lessonToTime.start"
                                                                         cacheName:nil];
    }
    return self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";


    // Configure the cell...
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    Lessons *lesson = [self.controller objectAtIndexPath:indexPath];
    cell.textLabel.text = lesson.lesson;


    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
    // This is the first lesson in this section:
    Lessons *lesson = [sectionInfo objects][0];

    //For testing I changed the type of 'start' and 'end' to string
    NSLog(@"start: %@",lesson.lessonToTime.start);
    NSLog(@"end: %@", lesson.lessonToTime.end );

    NSString *title = [NSString stringWithFormat:@"%@-%@",
                       lesson.lessonToTime.start,
                       lesson.lessonToTime.end];
    return title;
}

結果: 結果

4

2 に答える 2

2

まず、選択した日のレッスンのみが表示される述語を追加する必要があります。

NSPredicate *pred = [NSPredicate predicateWithFormat:@"lessonToDay.day == %@", theSelectedDay];
fetch.predicate = pred;

テーブル ビューをセクションにグループ化するsectionNameKeyPath:には、フェッチされた結果コントローラーのパラメーターをに設定する必要があります@"lessonToTime.start"。これにより、同じ開始時間のすべてのレッスンが 1 つのセクションにグループ化されます。(あなたのコメントを理解しているように、開始時刻が同じレッスンは終了時刻も同じなので、これで十分です。)最初のソート記述子として同じキーパスを使用する必要があります。

NSSortDescriptor *sortTime = [NSSortDescriptor sortDescriptorWithKey:@"lessonToTime.start" ascending:YES];
NSSortDescriptor *sortGroup = [NSSortDescriptor sortDescriptorWithKey:@"lesson" ascending:YES];
fetch.sortDescriptors = @[sortTime, sortGroup];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch
            managedObjectContext:self.managedObjectContext
            sectionNameKeyPath:@"lessonToTime.start"
            cacheName:nil];

必要に応じてセクション ヘッダーを表示するには、以下をオーバーライドする必要がありますtitleForHeaderInSection:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
    // This is the first lesson in this section:
    Lesson *lesson = [sectionInfo objects][0];
    // Now build some title from lesson.lessonToTime.start and lesson.lessonToTime.end:
    NSString *title = ...;
    return title;
}
于 2013-10-07T09:03:21.880 に答える
1

これは、2 つの View Controller で行うことができます。

  • DaysViewController- ここでは、すべての日を表示します。ユーザーがセルを選択すると、セルに関連付けられた日が取得され、それが 2 番目のビュー コントローラーに渡されます。
  • LessonsViewControllerNSFetchRequest- ここでは、渡されたオブジェクトを使用してを構築しますpredicate

更新:問題の説明が変更されたため、私の答えはもう正しくありません。

于 2013-10-07T08:29:53.050 に答える