2013-04-01 29 views
4

我想這段代碼如何重裝將在覈心數據

- (void)insertNewObject: (NSArray *) userInfo // Заносит блюдо в локальную базу данных 
{ 
    for (int i = 0; i < userInfo.count; i++) { 
     billContent * bc = [userInfo objectAtIndex:i]; 
     NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
     NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; 
     NSManagedObject *nmo = [NSEntityDescription insertNewObjectForEntityForName:[entity name] 
                     inManagedObjectContext:context]; 
     [nmo setValue:CountID forKey:@"billId"]; 
     [nmo setValue:bc.billAmount forKey:@"courseCount"]; 
     [nmo setValue:bc.billCourseId forKey:@"courseId"]; 
     [nmo setValue:bc.billPrice forKey:@"coursePrice"]; 
     [nmo setValue:bc.billTitle forKey:@"courseTitle"]; 
     [self saveContext]; 
    } 
} 

- (void)saveContext { 
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
    NSError *error = nil; 
    if (![context save:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
} 

插入在覈心數據的一些對象怎麼看這個物體​​在我的表視圖對象後表視圖?

我試圖從核心數據阿恩獲取,插入後,這段代碼

[self makeAList]; 

這裏

-(void) makeAList { 
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0]; 
for (int i = 0; i < [sectionInfo numberOfObjects]; i ++) { 
    NSIndexPath *ip = [NSIndexPath indexPathForRow: i inSection:0]; 
    billContent *bc = [[billContent alloc] init]; 
    NSManagedObject *mo = [self.fetchedResultsController objectAtIndexPath:ip]; 
    bc.billId = [[mo valueForKey:@"billId"] description]; 
    bc.billTitle = [[mo valueForKey:@"courseTitle"] description]; 
    bc.billPrice = [[mo valueForKey:@"coursePrice"] description]; 
    bc.billAmount = [[mo valueForKey:@"courseCount"] description]; 
    bc.depId = [[mo valueForKey:@"departmentId"] description]; 
    bc.billCourseId = [mo valueForKey:@"courseId"]; 
    [saved addObject:bc]; 
} 
[countView1 reloadData]; 

}

但沒有任何工程。但是,如果我脫離這個類,並再次輸入(加載viewDidLoad),我可以看到新的值,我做錯了什麼?

回答

7

你應該重寫fetchedResultController中的一些標準方法。 看一下下面的代碼: -

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

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

-(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: { 
     Course *changedCourse = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.textLabel.text = ...; 
    } 
     break; 

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

}

-(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; 
} 

}

+0

我有這樣的錯誤 錯誤:嚴重的應用程序錯誤。在調用-controllerDidChangeContent:期間,NSFetchedResultsController的委託捕獲到異常。無效更新:部分0中的行數無效。更新(6)後現有部分中包含的行數必須等於更新前(6)部分中包含的行數,加上或減去數字插入或從該部分刪除的行(插入1個,刪除0個)以及加上或減去移入或移出該部分的行數(移入0,移出0)。與userInfo(空) – Arthur

+0

我已經做到了)))謝謝你的幫助) – Arthur

+0

這太棒了! :) –