gpt4 book ai didi

ios - Core-Data NSFetchedResultsController 对多关系

转载 作者:行者123 更新时间:2023-12-01 19:06:37 25 4
gpt4 key购买 nike

这是我的数据模型:
Datamodel

我用数据填充了数据库。

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

然后我想在右侧部分显示类(class)数据。如何使用 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();
}

编辑:

这是 Storyboard:

Storyboard

更新

这是我的代码:

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;
}

结果:
Result

最佳答案

首先,您必须添加一个谓词,仅显示所选日期的类(class):

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

要将表格 View 分组为多个部分,您必须设置 sectionNameKeyPath:范围
获取的结果 Controller ,例如到 @"lessonToTime.start" .这将组
所有具有相同开始时间的类(class)都归为一个部分。
(据我了解您的评论,具有相同开始时间的类(class)也有相同的结束时间,
所以这应该足够了。)
必须使用相同的键路径作为第一个排序描述符:
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;
}

关于ios - Core-Data NSFetchedResultsController 对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19219841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com