2011-01-10 72 views
0

我无法弄清楚如何行计数是正确的,但uitableview不会加载行内容NSLog显示carresults =(null),但行数是正确的,在模拟器上,如果我重新启动,结果得到填充。这似乎是我第一次错过了我的第一个fetchedResultsController,但是如果它不知道那里有什么,它怎么能得到行计数呢?fetchedresults不会显示行内容,只有titleforheaders和rowsinSection在UITableView

帮助!!有任何想法吗?谢谢,迈克

的titleForHeaderInSection工作正常,带回正确的标题:

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

这带回正确的行数:

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

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

这不填充细胞,直到重建上模拟器,永远不会填充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

既然你没有提供任何调试信息我只能猜测,什么是错的,所以我会问你一些问题。它是否实际上用nib实例化firstviewCell属性。如果单元格未连接到文件所有者的firstviewCell属性(在笔尖中),它将不起作用。否则,如果您尝试访问数据,则fetchedResultsController中没有任何内容,您将收到错误消息。如果nslog被触发,那么你可能没有得到一个错误,这意味着你的Cars对象正在被抓取,而他们中没有任何东西。看看fetchedResultsController调用NSLog(@“Fetched Objects:%@”,[[fetchedResultsController fetchedObjects] description]);请记住,只有当您调用performFetch时,fetchedObjects才会更新。既然你说当你重新启动应用程序时,获取结果会被填充,你可能需要调用saveContext来获得加载的结果。唯一的原因是如果您在运行时创建数据,然后加载表视图。否则,我会假设你将表视图设置为抓取结果控制器的委托,以便它获知任何更改并作出适当的响应。应用程序委托通常在applicationWillResign active或applicationWillTerminate上执行此操作(applicationWillTerminate在正常关闭期间似乎不会被iOS4调用)。我唯一能想到的其他事情是,也许您的SectionInfo对象可能包含错误信息,请尝试调试太。

好运,

丰富

编辑:我appologize,保存上下文方法是一种添加到您的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,我把代码放在我编辑的问题底部。我会查找“saveContext”,但我没有在CoreDataBooks或CoreDataRecipes示例中看到它。谢谢 – 2011-01-11 00:42:05