0

シナリオ :

経費追跡 iOS アプリケーションがあり、経費詳細ビュー コントローラからの経費を、カテゴリと金額とともに経費のリストを表示するテーブル ビューに保存しています。

テーブルビューの上部には、CALENDAR ボタン、日付を示す UILabel テキスト (例: 2012 年 10 月 23 日 (日))、および側面に 2 つのボタンを備えた UIView があります。カレンダー ボタンを押すと、現在の日付でカスタム カレンダーが開きます。2 つのボタンは、それに応じて日付を減らしたり増やしたりするためのものです。

コア データ エンティティ「経費」の属性である日付に従って経費を保存したいと考えています。

質問: カレンダー ボタンを押して、そこからランダムな日付を選択するとします。その下のテーブル ビューに、その日の特定の費用が表示されます。私が言いたいのは、テーブル ビューに特定の日付の費用だけを表示したいということです。日付をインクリメントまたはデクリメントするボタンを押すと、テーブル ビューにその日の費用が表示されます。費用を節約するために、NSFetchedResultsController と Core Data を使用しています。

これを達成する方法について何か考えはありますか?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;
}

みんなありがとう。

4

1 に答える 1

0

適切に設定されNSFetchedResultsControllerた新しいもので新しいものを作成する必要があります:NSFetchRequestNSPredicate

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に割り当てられます。述語はフィルターと考えることができます。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateToFilterFor];

これを呼び出してフェッチ要求に追加する場合は、フェッチされた要求に、述語で指定した日付と一致する[fetchRequest setPredicate:predicate];日付プロパティの結果のみをフェッチするように指示します。NSManagedObjectこれはまさにあなたがここで望むものです。

したがって、ユーザーが日付を選択した後に呼び出されるメソッドがある場合は、次のように変更できます。

- (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を使用していない場合(使用する必要があります)、割り当てられたオブジェクトを適切に解放する必要があることに注意してください。

于 2012-10-28T11:24:28.573 に答える