0

Ich kann nicht herausfinden, wie die Zeilenanzahl korrekt ist, aber die uitableview lädt nicht den Inhalt der Zeile NSLog zeigt carsresults = (null), aber die Anzahl der Zeilen ist korrekt, beim Relaunch am Simulator werden die Ergebnisse ausgefüllt. Es scheint so, als ob ich meinen ersten geholtenResultsController beim ersten Mal vermisse, aber wie kann es die Zeilenzählung bekommen, wenn es nicht weiß, was da ist?fetchedresults zeigt keine Zeileninhalte an, nur titleforheaders und rowsinSection in UITableView

Hilfe !! irgendwelche Ideen? Danke, Mike

Die titleForHeaderInSection funktioniert gut, bringt die richtigen Titel zurück:

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

Dies bringt die richtige Zeilenanzahl zurück:

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

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

Das die Zellen nicht bevölkern, bis ein Wiederaufbau auf Simulator, füllt nie das 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); 

EDIT: Hier ist die FRC:

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

Antwort

0

Da Sie keine Debug-Informationen zur Verfügung gestellt haben kann ich nur so erraten, was falsch ist, so werde ich Ihnen ein paar Fragen stellen. Wird die Eigenschaft firstviewCell tatsächlich mit der Spitze instanziiert? Wenn die Zelle nicht mit der Eigenschaft firstviewCell des Dateieigentümers (in der Nib) verbunden ist, funktioniert sie nicht. Andernfalls, wenn der abgerufeneResultsController nichts enthält, erhalten Sie eine Fehlermeldung, wenn Sie versuchen, auf die Daten zuzugreifen. Wenn das nslog ausgelöst wird, hast du wahrscheinlich keinen Fehler erhalten, was bedeutet, dass deine Cars-Objekte geholt werden, da ist nichts in ihnen. Um zu sehen, was in der abgerufenenResultsController Aufruf NSLog (@ "abgerufene Objekte:% @", [[holedResultsController abgerufeneObjekte] Beschreibung]); Beachten Sie jedoch, dass holedObjects nur aktualisiert wird, wenn Sie performFetch aufrufen. Da Sie sagen, dass carresults gefüllt wird, wenn Sie die App neu starten, müssen Sie möglicherweise saveContext aufrufen, damit die Ergebnisse geladen werden. Der einzige Grund dafür ist, dass Sie die Daten zur Laufzeit erstellen, bevor die Tabellenansicht geladen wird. Andernfalls würde ich annehmen, dass Sie die Tabellenansicht als Delegierten des abgerufenen Ergebniscontrollers festgelegt haben, so dass sie über Änderungen informiert wird und entsprechend reagiert. Der App-Delegierte tut dies normalerweise auf applicationWillResign aktiv oder applicationWillTerminate (applicationWillTerminate scheint beim normalen Schließen nicht von iOS4 aufgerufen zu werden). Die einzige andere Sache, die mir in den Sinn kommt, ist, dass Ihr SectionInfo-Objekt möglicherweise die falschen Informationen enthält auch.

Good Luck,

Rich-

Edit: Ich entschuldige, die Kontext-Methode speichern ist eine Methode, um Ihre AppDelegate hinzugefügt, wenn Sie eine App erstellen, basierend auf Kerndaten. Eine gute Möglichkeit, einen Core-Datenstapel in ein NSObject zu schreiben, kann nützlich sein, um es zu einem Singleton zu machen, es sei denn natürlich, Sie benötigen Parallelität. In diesem Fall wird es wirklich kompliziert. Dies ist die Implementierung einschließlich der Kontextfunktion speichern:

// 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

Hallo, Antworten: Ja, wird die Zelle verbunden. Die Ergebnisse Ihres Protokollvorschlags waren (null). Da ist ein frc.delgate = selbst, ich habe den Code am Ende meiner Frage unter EDIT gestellt. Ich werde nach dem "saveContext" suchen, aber ich sehe es nicht in den Beispielen CoreDataBooks oder CoreDataRecipes.Danke –

+0

Aus der Dokumentation von Apple: "Wenn Sie einfach ein verwaltetes Objekt erstellen, wird es nicht in einem persistenten Speicher gespeichert. Es wird einfach dem Kontext des verwalteten Objekts zugeordnet. Um Änderungen an den Speicher zu übermitteln, senden Sie dem Kontext eine Nachricht save:. " Meine beste Vermutung ist, dass wenn Sie nichts in dem abgerufenen Ergebnis-Controller haben, Sie entweder nach dem Erstellen der Abschnitte nicht darauf zugreifen, oder Sie den Kontext nicht gespeichert haben. – Rich

+0

Danke. Ich habe es in Gang gebracht. –