gpt4 book ai didi

ios - 在核心数据中插入对象后如何重新加载 TableView

转载 作者:可可西里 更新时间:2023-11-01 05:22:58 25 4
gpt4 key购买 nike

我试图通过这段代码在核心数据中插入一些对象

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

如何在我的 TableView 中查看此对象?

我试图通过这段代码在插入后再次从核心数据中获取

[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),我可以看到新的值,我做错了什么?

最佳答案

您应该覆盖 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;
}

关于ios - 在核心数据中插入对象后如何重新加载 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com