Ios 具有节名称的NSFetchedResultsController
情景: 我有一个费用跟踪iOS应用程序,我正在将费用从费用详细信息视图控制器存储到一个表视图(带有“获取结果”控制器),该表视图显示费用列表以及类别、金额和日期。我的实体“Money”中有一个日期属性,它是费用或收入的母实体 我的问题: 我想要的是基本上对给定的一周、一个月或一年的费用/收入进行分类,并将其显示为部分标题,例如:(2012年10月1日至10月7日),它根据特定的一周显示费用/收入金额和相关内容。该视图中提供了两个按钮,如果我按下右键,它将增加一周(2012年10月1日至10月7日现在显示2012年10月8日至10月15日),同样,左键将减少一周。我在视图中也有两个线段控件。我想做的是按下显示“每周”的段控制键,如果我按下另一个显示“类别”的段,我将如何根据类别筛选出我每周的支出/收入 我想在我的表视图中显示两个部分(一个显示开支日期,另一个以格式显示收入日期(2012年10月12日-2012年10月7日))。我将如何实现这一点?我已经写了一些伪代码,如果有人能告诉我如何完成上述任务,那就太好了 编辑-提取控制器Ios 具有节名称的NSFetchedResultsController,ios,core-data,nsfetchedresultscontroller,nsfetchrequest,Ios,Core Data,Nsfetchedresultscontroller,Nsfetchrequest,情景: 我有一个费用跟踪iOS应用程序,我正在将费用从费用详细信息视图控制器存储到一个表视图(带有“获取结果”控制器),该表视图显示费用列表以及类别、金额和日期。我的实体“Money”中有一个日期属性,它是费用或收入的母实体 我的问题: 我想要的是基本上对给定的一周、一个月或一年的费用/收入进行分类,并将其显示为部分标题,例如:(2012年10月1日至10月7日),它根据特定的一周显示费用/收入金额和相关内容。该视图中提供了两个按钮,如果我按下右键,它将增加一周(2012年10月1日至10月7日
- (void)userDidSelectStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate
{
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Money" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[NSFetchedResultsController deleteCacheWithName:nil];
//Here you create the predicate that filters the results to only show the ones with the selected date
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];
[fetchRequest setPredicate:predicate];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor * sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSSortDescriptor *sortDescriptor3 = [[NSSortDescriptor alloc] initWithKey:@"cat" ascending:YES];
NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, sortDescriptor3, nil];
[fetchRequest setSortDescriptors:descriptors];
[fetchRequest setIncludesSubentities:YES];
[fetchRequest setResultType:NSManagedObjectResultType];
if(_fetchedResultsController)
{
[_fetchedResultsController release]; _fetchedResultsController = nil;
}
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"type" cacheName:nil];
_fetchedResultsController.delegate = self;
NSError *anyError = nil;
if(![_fetchedResultsController performFetch:&anyError])
{
NSLog(@"error fetching:%@", anyError);
}
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptor3 release];
[fetchRequest release];
//Finally you tell the tableView to reload it's data, it will then ask your NEW FRC for the new data
[self.dashBoardTblView reloadData];
}
-(void)userDidSelectStartDate:(NSDate*)startDate和endDate:(NSDate*)endDate
{
AppDelegate*applicationDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[ApplicationLegate managedObjectContext];
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
//根据需要编辑实体名称。
NSEntityDescription*entity=[NSEntityDescription entityForName:@“Money”在托管对象上下文:上下文中];
[FetchRequestSetEntity:entity];
[NSFetchedResultsController deleteCacheWithName:nil];
//在这里,您将创建一个谓词,用于过滤结果,以仅显示具有选定日期的结果
NSPredicate*谓词=[NSPredicate谓词WithFormat:@”(日期>=%@)和(日期要将“费用”/“收入”对象分组到不同的部分,您需要向货币
实体添加类型
属性,例如整数
属性,对于费用为0
,对于收入为1
要按类型(费用/收入)对表视图部分进行排序,请使用type
作为第一个排序描述符:
NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
[fetchRequest setSortDescriptors:descriptors];
要按日期对每个部分中的条目进行排序,请使用date
作为第二个排序描述符:
NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
[fetchRequest setSortDescriptors:descriptors];
最后,要按类别将表视图分组为多个部分,请使用type
作为sectionNameKeyPath
:
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:type
cacheName:nil];
这应该分为两个部分,所有费用在第一部分,所有收入在第二部分
现在,对于部分标题,您必须实现tableView:viewForHeaderInSection:
(我希望我能做到这一点):
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
Money*Money=[[sectionInfo objects]objectAtIndex:0];//此节中的第一个对象
NSNumber*type=money.type;
if(type.intValue==0){
//创建并返回“费用”部分的标题视图。
}否则{
//创建并返回“收入”部分的标题视图。
}
}
要将“费用”/“收入”对象分组到不同的部分,您需要向货币
实体添加类型
属性,例如整数
属性,对于费用为0
,对于收入为1
要按类型(费用/收入)对表视图部分进行排序,请使用type
作为第一个排序描述符:
NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
[fetchRequest setSortDescriptors:descriptors];
要按日期对每个部分中的条目进行排序,请使用date
作为第二个排序描述符:
NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
[fetchRequest setSortDescriptors:descriptors];
最后,要按类别将表视图分组为多个部分,请使用type
作为sectionNameKeyPath
:
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:type
cacheName:nil];
这应该分为两个部分,所有费用在第一部分,所有收入在第二部分
现在,对于部分标题,您必须实现tableView:viewForHeaderInSection:
(我希望我能做到这一点):
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
Money*Money=[[sectionInfo objects]objectAtIndex:0];//此节中的第一个对象
NSNumber*type=money.type;
if(type.intValue==0){
//创建并返回“费用”部分的标题视图。
}否则{
//创建并返回“收入”部分的标题视图。
}
}
谢谢,但我想让我的分区名称显示当前周(日期),而行将显示类别名称。@AngadManchanda:然后我没有正确理解你的问题。你说你想要两个分区(一个用于费用,一个用于收入)-也许你可以举一个例子来说明这个表应该是什么样子的。我想要两个部分。一个红色的部分会有本周的标题(日期像2012年10月1日-2012年10月7日-这是部分标题)行将是类别并显示费用金额。类似地,另一个以绿色显示当前周的部分作为部分标题,行显示带有收入金额的类别。属性“cat”位于“Money”实体中,该实体是“expense”和“income”的父实体“实体。现在,清楚了吗?你能帮忙吗?@AngadManchanda:好的。但是你需要“货币”实体中的一些属性,例如支出的0
,收入的1
。其他