0

行数がどのように正しいかわかりませんが、NSLogがcarresults =(null)を示す行の内容を読み込めません。行数が正しいです、私が再起動すると、シミュレータ上で、carresultsがいっぱいになる。私は最初のfetchedResultsControllerを初めて見逃しているようですが、何があったのかわからない場合は、どのように行数を取得できますか?fetchedresultsは行の内容を表示せず、UITableViewのtitleforheadersとrowsinSectionだけを表示します。

ヘルプ!何か案は?上の再構築まで、これが細胞に移入されません

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController1 sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
    }  

:これは正しい行数を戻します

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [[[fetchedResultsController1 sections] objectAtIndex:section] name]; 
    }  

:おかげで、マイク

titleForHeaderInSectionが正常に動作しますが、正しいタイトルに戻しますシミュレータは、決してiPhoneに値を入力しません。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *FirstViewIdentifier = @"FirstViewIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstViewIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
     cell = firstviewCell; 
     self.firstviewCell = nil; 
    } 

    Cars *carresults = (Cars *)[fetchedResultsController1 objectAtIndexPath:indexPath]; 

NSLog(@"carresults %@", carresults.make); 

編集:ここでは、FRCは次のとおりです。

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[[UIApplication sharedApplication] delegate] managedObjectContext] sectionNameKeyPath:@"key" cacheName:@"Root1"]; 
self.fetchedResultsController = aFetchedResultsController; 
fetchedResultsController.delegate = self; 

答えて

0

あなたは私だけ間違っているものにと推測することができます任意のデバッグ情報を提供していないので、私はあなたにいくつか質問をしますので。それは実際にペン先でfirstviewCellプロパティをインスタンス化しますか?セルが(Nib内の)File OwnerのfirstviewCellプロパティに接続されていないと、動作しません。それ以外の場合、fetchedResultsControllerに何も格納されていないと、データにアクセスしようとするとエラーが発生します。 nslogが起動した場合、おそらくエラーは発生しませんでした。つまり、Carsオブジェクトはフェッチされています。 fetchedResultsControllerのNSLog(@ "フェッチされたオブジェクト:%@"、[[fetchedResultsController fetchedObjects] description])の内容を確認します。ただし、fetchedObjectsはperformFetchを呼び出すときにのみ更新されることに注意してください。アプリを再起動するとcarresultsがいっぱいになると言うので、結果をロードするためにsaveContextを呼び出す必要があるかもしれません。唯一の理由は、テーブルビューがロードされる前に、実行時にデータを作成する場合です。それ以外の場合は、テーブルビューをフェッチされた結果コントローラのデリゲートとして設定して、変更が通知され、適切に応答すると仮定します。アプリケーションの代理人は通常、applicationWillResign activeまたはapplicationWillTerminateでこれを行います(applicationWillTerminateは正常終了時にiOS4によって呼び出されないようです)。私が考えることができる唯一のものは、SectionInfoオブジェクトに間違った情報が含まれている可能性があります。あまりにも。

グッドラック、

リッチ

編集:ごめんなさい、保存コンテキストメソッドを使用すると、コアデータに基づいてアプリケーションを作成するときの方法は、あなたのappdelegateに追加されます。コアデータスタックを作る良い方法は、NSObjectにラップすることです。もちろん、同時実行性が必要な場合を除いて、シングルトンにするのは便利です。その場合、実際には複雑になります。これは、保存コンテキスト機能を含む実装です:

// CoreDataStack.h 
// do not call alloc, retain, release, copy or especially copyWithZone: (because I didn't bother to override it since you shouldn't try to create this in anything but the main thread, and definatly don't dispatchasync this object's methods) 

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
#define kYourAppName @"This should be replaced by the name of your datamodel" 

@interface CoreDataStack : NSObject { 

@private 
    NSManagedObjectContext *managedObjectContext_; 
    NSManagedObjectModel *managedObjectModel_; 
    NSPersistentStoreCoordinator *persistentStoreCoordinator_; 

} 

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; 
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

+ (CoreDataStack *)sharedManager; 
+ (void)sharedManagerDestroy; 

// call this in your app delegate in applicationWillTerminate and applicationWillResignActive 
- (void)saveContext; 

- (NSURL *)applicationLibraryDirectory; 

@end 

// CoreDataStack.m

#import "CoreDataStack.h" 

    @interface CoreDataStack() 
     - (oneway void)priv_release; 
    @end 

    @implementation CoreDataStack 

    static CoreDataStack *sharedManager = nil; 

    + (CoreDataStack *)sharedManager { 
     if (sharedManager != nil) { 
      return sharedManager; 
     } 
     sharedManager = [[CoreDataStack alloc] init]; 
     return sharedManager; 
    } 

    + (void)sharedManagerDestroy { 
     if (sharedManager) { 
      [sharedManager priv_release]; 
      sharedManager = nil; 
     } 
    } 

    - (id)retain { 
     return self; 
    } 

    - (id)copy {return self;} 

    - (oneway void)release{} 

    - (oneway void)priv_release { 
     [super release]; 
    } 

    - (void)saveContext { 

     NSError *error = nil; 
     if (managedObjectContext_ != nil) { 
      if ([managedObjectContext_ hasChanges] && ![managedObjectContext_ save:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
       */ 
       //abort(); 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                   message:@"The app has run into an error trying to save, please exit the App and contact the developers. Exit the program by double-clicking the home button, then tap and hold the iMean icon in the task manager until the icons wiggle, then tap iMean again to terminate it" 
                   delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
       [alert show]; 
       [alert release]; 
      } 
     } 
    } 

    #pragma mark - 
    #pragma mark Core Data stack 

    /** 
    Returns the managed object context for the application. 
    If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
    */ 
    - (NSManagedObjectContext *)managedObjectContext { 

     if (managedObjectContext_ != nil) { 
      return managedObjectContext_; 
     } 

     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
     if (coordinator != nil) { 
      managedObjectContext_ = [[NSManagedObjectContext alloc] init]; 
      [managedObjectContext_ setPersistentStoreCoordinator:coordinator]; 
     } 
     return managedObjectContext_; 
    } 


    /** 
    Returns the managed object model for the application. 
    If the model doesn't already exist, it is created from the application's model. 
    */ 
    - (NSManagedObjectModel *)managedObjectModel { 

     if (managedObjectModel_ != nil) { 
      return managedObjectModel_; 
     } 
     NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"kYourAppName" withExtension:@"momd"]; 
     managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  
     return managedObjectModel_; 
    } 


    /** 
    Returns the persistent store coordinator for the application. 
    If the coordinator doesn't already exist, it is created and the application's store added to it. 
    */ 
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

     if (persistentStoreCoordinator_ != nil) { 
      return persistentStoreCoordinator_; 
     } 
     NSString *yourAppName = [[NSString stringWithFormat:@"%@.sqlite",kYourAppName] autorelease]; 
     NSURL *storeURL = [[self applicationLibraryDirectory] URLByAppendingPathComponent:yourAppName]; 

     NSError *error = nil; 
     persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
     if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

      Typical reasons for an error here include: 
      * The persistent store is not accessible; 
      * The schema for the persistent store is incompatible with current managed object model. 
      Check the error message to determine what the actual problem was. 


      If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

      If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
      * Simply deleting the existing store: 
      [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

      * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
      [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

      Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      // abort(); 

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"The app has run into an error trying to load it's data model, please exit the App and contact the developers. Exit the program by double-clicking the home button, then tap and hold the iMean icon in the task manager until the icons wiggle, then tap iMean again to terminate it" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     }  

     return persistentStoreCoordinator_; 
    } 


    #pragma mark - 
    #pragma mark Application's Library directory 

    /** 
    Returns the URL to the application's Documents directory. 
    */ 
    // returns the url of the application's Library directory. 
    - (NSURL *)applicationLibraryDirectory { 
     return [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; 
    } 


    #pragma mark - 
    #pragma mark Memory management 

    - (void)dealloc { 
     // release and set all pointers to nil to avoid static issues 
     [managedObjectContext_ release]; 
     managedObjectContext_ = nil; 
     [managedObjectModel_ release]; 
     managedObjectModel_ = nil; 
     [persistentStoreCoordinator_ release]; 
     persistentStoreCoordinator_ = nil; 

     [super dealloc]; 
    } 

    @end 
+0

こんにちは、答え:はい、セルが接続されています。あなたのログ提案の結果は(null)でした。 frc.delgate = selfがあります。私はEDITの下に私の質問の一番下にコードを置いています。私は "saveContext"を探しますが、CoreDataBooksやCoreDataRecipesの例では表示されません。ありがとうございました –

+0

アップルのドキュメントから:「単に管理対象オブジェクトを作成しても、それが永続ストアに保存されるわけではなく、管理対象オブジェクトコンテキストに関連付けられているだけです。 "私の最高の推測は、フェッチされた結果コントローラに何もない場合、あなたのセクションを作成した後にperformFetchを実行しなかったか、またはコンテキストを保存しなかったかのいずれかです。 – Rich

+0

ありがとうございます。私はそれを行った。 –

関連する問題