我用数据填充了数据库。
现在我想在表格视图中显示它。首先,我想选择具有“天”属性的一天,然后学习它的课程。我想在标题 ( -(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;
}
结果: