シナリオ:
私は経費追跡 iOS アプリケーションを持っており、経費詳細ビュー コントローラからテーブル ビュー (フェッチされた結果コントローラを含む) に経費を保存しています。テーブル ビューには、カテゴリ、金額、日付とともに経費のリストが表示されます。費用または収入のいずれかの親エンティティであるエンティティ「Money」に日付属性があります。
私の質問:
私が望むのは、基本的に、特定の週、月、または年の支出/収入を分類し、セクション ヘッダー タイトルとして表示することです (例: (2012 年 10 月 1 日 - 10 月 7 日))。その特定の週に応じたもの。そのビューには 2 つのボタンが用意されています。右のボタンを押すと、週が 1 週間ずつ増え (2012 年 10 月 1 日から 10 月 7 日までは 2012 年 10 月 8 日から 10 月 15 日までが表示されます)、同様に左のボタンで週が減ります。 1週間で。ビューにも 2 つのセグメント コントロールがあります。私がやりたいのは、「毎週」と表示されているセグメント コントロールを押すことです。「カテゴリ」と表示されている別のセグメントを押すと、カテゴリに基づいて 1 週間あたりの費用/収入をどのように除外しますか?
テーブル ビューに 2 つのセクションを表示したいと考えています (1 つは支出の日付を表示し、もう 1 つは収入の日付を形式で表示します (2012 年 10 月 1 日 - 2012 年 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];
}
EDIT - コントローラーデリゲートメソッドの取得
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
[self.dashBoardTblView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.dashBoardTblView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.dashBoardTblView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
{
[self.dashBoardTblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.dashBoardTblView endUpdates];
}