2013-04-01 28 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

这太棒了! :) –