2011-02-25 40 views
0

我正在爲iPad開發應用程序,但無法顯示已保存在覈心數據中的數據。我早些時候問過這個問題,很多人幫助過我,但我仍然遇到同樣的問題。這一次,在調試的時候,我得到了錯誤,我設置的數組接收取回的對象無法訪問。請再次幫助我,因爲這很緊急。我的代碼如下...核心數據:無法訪問通過獲取結果返回的託管對象控制器

#import "RootViewController.h" 
    #import "DetailViewController.h" 
    #import "AddViewController.h" 
    #import "EmployeeDetailsAppDelegate.h" 

/* 
This template does not ensure user interface consistency during editing operations in the table view. You must implement appropriate methods to provide the user experience you require. 
*/ 

@interface RootViewController() 
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; 
@end 



@implementation RootViewController 

@synthesize detailViewController, fetchedResultsController, managedObjectContext, array, dictionary; 
//@synthesize array; 

#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 

    self.title = @"Employee Name"; 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    [super viewDidLoad]; 

    if (self.fetchedResultsController) 
    { 
     NSArray *tempArr = self.fetchedResultsController.fetchedObjects; 
     NSLog(@"temp array is %@",tempArr); 
     int count =[tempArr count]; 
     int i; 
     for(i=0;i <count;i++) 
     { 
      NSManagedObject *category = [tempArr objectAtIndex:i]; 
      dictionary = [[NSMutableDictionary alloc] initWithCapacity:0]; 

      if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]]) 
      { 
       [dictionary setObject:[category valueForKey:@"EmployeeName"] forKey:@"EmployeeName"]; 
      } 

      if([[category valueForKey:@"EmployeeID"] isKindOfClass:[NSString class]]) 
      { 
       [dictionary setObject:[category valueForKey:@"EmployeeID"] forKey:@"EmployeeID"]; 
      } 
      if([[category valueForKey:@"EmployeeDepartment"] isKindOfClass:[NSString class]]) 
      { 
       [dictionary setObject:[category valueForKey:@"EmployeeDepartment"] forKey:@"EmployeeDepartment"]; 
      } 

      [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"EmployeeName"]; 

     [self.array addObject:dictionary]; 

     [dictionary release]; 
     [tempArr release]; 
     } 
    } 
} 




- (void)viewWillAppear:(BOOL)animated { 

    [self.tableView reloadData]; 
// [super viewWillAppear:animated]; 
} 


/* 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 

*/ 

/* 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 
*/ 

/* 
- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 
*/ 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.  
    return YES; 
} 

#pragma mark - 
#pragma mark Add a new object 

- (void)insertNewObject:(id)sender { 

    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil]; 
    self.modalPresentationStyle = UIModalPresentationFormSheet; 
    add.wantsFullScreenLayout = NO; 

    [self presentModalViewController:add animated:YES]; 
    [add release]; 
} 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


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

// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
// return [sectionInfo numberOfObjects]; 
    if([self.array count]) 
    { 
     return [array count]; 
    } 
    return 1; 
} 


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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 

// [self configureCell:cell atIndexPath:indexPath]; 

    if(dictionary = [array objectAtIndex:indexPath.row]) 
    { 
     [dictionary objectForKey:@"EmployeeName"]; 
     cell.textLabel.text = @"(EmployeeName = %@)";   
    } 

    else { 
     cell.textLabel.text = @"No Employee Name"; 
    } 


    return cell; 
} 

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{ 

// AddViewController *detail = [fetchedResultsController objectAtIndexPath:indexPath]; 
// cell.textLabel.text = detail.empName.text; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     // Delete the managed object. 
     NSManagedObject *objectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     if (self.detailViewController.detailItem == objectToDelete) { 
      self.detailViewController.detailItem = nil; 
     } 

     NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
     [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

     NSError *error; 
     if (![context 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. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // The table view should not be re-orderable. 
    return NO; 
} 


#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Set the detail item in the detail view controller. 

    detailViewController = [[DetailViewController alloc]initWithStyle:UITableViewStylePlain]; 
// AddViewController *selectedName = (AddViewController *)[[self fetchedResultsController]objectAtIndexPath:indexPath]; 
// detailViewController.detail = selectedName; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
} 


#pragma mark - 
#pragma mark Fetched results controller 

- (NSFetchedResultsController *)fetchedResultsController { 

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

    /* 
    Set up the fetched results controller. 
    */ 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:20]; 

    // Edit the sort key as appropriate. 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"EmployeeName" ascending:NO]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

    AddViewController *name = [[AddViewController alloc]init]; 

    if (name.empName.text) { 
     NSPredicate *inPredicate = [NSPredicate predicateWithFormat: @"EmployeeName = %@", name.empName.text]; 
     [fetchRequest setPredicate:inPredicate]; 
    } 

    [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:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 

    [aFetchedResultsController release]; 
    [fetchRequest release]; 
    [sortDescriptors release]; 

    return fetchedResultsController; 
}  


#pragma mark - 
#pragma mark Fetched results controller delegate 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 
} 


#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc. that aren't in use. 
} 


- (void)viewDidUnload { 

    self.fetchedResultsController = nil; 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 

// self.array = nil; 
} 


- (void)dealloc { 

// [array release]; 
    [detailViewController release]; 
    [fetchedResultsController release]; 
    [managedObjectContext release]; 

    [super dealloc]; 
} 

@end

問候, Dipanjan

+0

請發佈較少的代碼。 – 2011-02-25 12:47:05

回答

1

首先,你不必要創造字典的數組,做無非是重複陣列已經獲取的成果控制器fetchedObjects返回。

您似乎想要檢查託管對象屬性的空值,但您的錯誤發生。這種類型的檢查:

if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]]) 

...將評估爲TRUE,即使因爲沒有值的屬性,因爲鍵 - 值編碼返回值將是一個字符串(因爲它總是返回一個字符串生成的訪問方法。

處理空字段是在數據模型本身最好的地方,只要設置屬性EmployeeName的默認值設置爲「無僱員姓名」,你可以與所有的檢查免除。

其次,你對數組使用不一致的引用,有時使用self.array和其他時間t array。只有在使用第一種形式時才能獲得適當的保留。如果你使用第二個,你的數組可能隨機消失。

第三,在tableView:(UITableView *)tableView cellForRowAtIndexPath:這行:

if(dictionary = [array objectAtIndex:indexPath.row]) 

...使用錯誤的接入形式,所以它可能會或可能無法找到數組對象。這條線:

cell.textLabel.text = @"(EmployeeName = %@)"; 

...設置所有單元格的文本爲「(EmployeeName =)」,並可能會產生編譯錯誤。它應該是:

cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[dictionary objectForKey:@"EmployeeName"]]; 

你真的應該只使用所取得的成果控制器和默認字段(如上)是這樣的:

NSManagedObject *mo=[[fetchedResultsController fetchedObjects] objectAtIndex:index.row]]; 
    cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[mo valueForKey:@"EmployeeName"]]; 

你不必做錯誤檢查,因爲數量表中的行總是與獲取的管理對象的數量相匹配。

我認爲你會讓這個過程複雜得多。您可能習慣使用SQL返回的數據進行大量的完整性檢查。但是,Core Data和提取的結果控制器將爲您處理大部分工作。在這樣一個簡單的設置中,您通常只需將大約十行代碼添加到tableview控制器模板中。

0

我不知道,但我認爲你的陣列出現故障,你所得到的元素的方式:

NSArray *tempArr = self.fetchedResultsController.fetchedObjects; 

你可以嘗試做一個for循環,並添加每個對象到陣列中。事情是這樣的:

NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

for(elementType *element in [self.fetchedResultsController fetchedObjects]) 
{ 
    [tempArray addObject: [element objectForKey: appropriateKey]]; 
} 
相關問題