核心数据和表格视图
场景:
我有一个跟踪iOS应用程序的费用,我将费用从一个费用明细视图控制器存储到一个表格视图,显示费用列表以及类别和金额。
在tableview的顶部,是一个带CALENDARbutton的UIView,一个显示date的UILabel文本(例如:2012年10月23日(星期日)),另外还有2个button。 按下日历button打开一个具有当前date的自定义日历,两个button用于相应地递减和递增date。
我想根据在我的核心数据实体“费用”中属性的date来节省费用。
问题:假设我按下日历button,并从那里select一些随机date,它下面的表格视图应该显示当天的特定费用。 我的意思是我希望表格视图只显示特定的date费用,如果我按下button来增加date或减lessdate,表格视图应该显示当天的费用。 我正在使用NSFetchedResultsController和核心数据,以节省我的开支。
任何想法,我将如何实现这一目标? 这是FRC的代码。
-(NSFetchedResultsController *)fetchedResultsController { if(_fetchedResultsController != nil) { return _fetchedResultsController; } AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = [applicationDelegate managedObjectContext]; NSFetchRequest * request = [[NSFetchRequest alloc]init]; [request setEntity:[NSEntityDescription entityForName:@"Money" inManagedObjectContext:context]]; NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"rowNumber" ascending:YES]; NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor1, nil]; [request setSortDescriptors: descriptors]; [request setResultType: NSManagedObjectResultType]; [request setIncludesSubentities:YES]; [sortDescriptor1 release]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; self.fetchedResultsController.delegate = self; [request release]; NSError *anyError = nil; if(![_fetchedResultsController performFetch:&anyError]) { NSLog(@"error fetching:%@", anyError); } return _fetchedResultsController; }
多谢你们。
你将不得不创build一个新的NSFetchRequest
与一个新的NSFetchedResultsController
有一个适当的设置NSPredicate
:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateToFilterFor]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setPredicate:predicate]; // ... NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"SomeCacheName"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController;
不要忘记调用[self.tableView reloadData];
分配新的FRC后。
编辑:您可以分配一个谓词到一个NSFetchRequest
然后分配给fetchedResultsController。 你可以把谓词看作一个filter。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateToFilterFor];
如果通过调用[fetchRequest setPredicate:predicate];
将其添加到获取请求中[fetchRequest setPredicate:predicate];
你告诉获取的请求只能获取NSManagedObject
date属性与你在谓词中提供的date相匹配的结果。 这正是你想要的。
所以如果你有一个方法在用户select一个date之后被调用,你可以像这样修改它:
- (void)userDidSelectDate:(NSDate *)date { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; //Here you create the predicate that filters the results to only show the ones with the selected date NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", date]; [fetchRequest setPredicate:predicate]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; NSArray *sortDescriptors = @[sortDescriptor]; [fetchRequest setSortDescriptors:sortDescriptors]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"]; aFetchedResultsController.delegate = self; //Here you replace the old FRC by this newly created self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } //Finally you tell the tableView to reload it's data, it will then ask your NEW FRC for the new data [self.tableView reloadData]; }
注意,如果你不使用ARC(你应该),你必须适当地释放分配的对象。