1

这是我的数据模型: 数据模型

我用数据填充了数据库。

现在我想在表格视图中显示它。首先,我想选择具有“天”属性的一天,然后学习它的课程。我想在标题 ( -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section)中显示属性 'end' 和 'start'

然后我想在右侧部分显示课程数据。如何使用 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". 这会将具有相同开始时间的所有课程归为一个部分。(据我了解您的评论,具有相同开始时间的课程也具有相同的结束时间,所以这应该足够了。)必须使用相同的键路径作为第一排序描述符:

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

您可以使用两个视图控制器来执行此操作:

  • DaysViewController- 在这里你显示所有的日子。当用户选择单元格时,您将获得与该单元格关联的日期并将其传递给第二个视图控制器,
  • LessonsViewController- 在这里你在构建你NSFetchRequest的时使用传递的对象predicate

更新:我的答案不再正确,因为问题的描述已经改变。

于 2013-10-07T08:29:53.050 回答