ios - 添加 CoreData 后更新 TableView

标签 ios uitableview core-data

我正在开发一个 CoreData 应用程序,它在 TableView 中显示 CoreData 的内容。 问题是,在添加字符串后,tableView 没有更新 CoreData 的内容。

我试过:

    - (void)viewWillAppear:(BOOL)none
{
    [self.tableView reloadData];
}

但没有机会,它没有重新加载。

有任何猜测或者我应该发布更多代码吗?谢谢:)

更多代码:)

将字符串保存到 CoreData:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];
    NSManagedObject *newProduct;
    newProduct = [NSEntityDescription
                  insertNewObjectForEntityForName:@"Products"
                  inManagedObjectContext:context];
    [newProduct setValue: _textField.text forKey:@"productName"];
    NSError *error;
    [context save:&error];
    NSLog(@"Product saved");
}

以及 TableViewController 的代码:

@implementation CartViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Core Data Versuche


    /*
     Fetch existing events.
     Create a fetch request for the Event entity; add a sort descriptor; then execute the fetch.
     */
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Products"];
    [request setFetchBatchSize:20];

    // Order the events by creation date, most recent first.
    // NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
    // NSArray *sortDescriptors = @[sortDescriptor];
    // [request setSortDescriptors:sortDescriptors];

    // Execute the fetch.
    NSError *error;
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSArray *fetchResult = [delegate.managedObjectContext executeFetchRequest:request error:&error];
    if (fetchResult== nil) {
        // 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.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    // Set self's events array to a mutable copy of the fetch results.
    [self setCartProductsArray:[fetchResult mutableCopy]];

    /*
     Reload the table view if the locale changes -- look at APLEventTableViewCell.m to see how the table view cells are redisplayed.
     */
    __weak UITableViewController *cell = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:NSCurrentLocaleDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

        [cell.tableView reloadData];
    }];

    // Ende Core Data Versuche
}
- (void)viewWillAppear:(BOOL)none
{
    [self.tableView reloadData];
}

最佳答案

您可能想要做的是使用 NSFetchedResultsController

  1. 确保在更新数据时,您实际上只是在更新 CoreData。
  2. 当 CoreData 发生变化并且您有一个 NSFetchedResultsController 时,您可以订阅更新。苹果很棒documentation关于如何使这项工作。这些更新将使用 delegation methods 正确更新您的表.这最终主要是从文档中复制和粘贴。

关于ios - 添加 CoreData 后更新 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22365547/

相关文章:

ios - 如何在 UITable 中获取 UIButton 或 UILabel 的位置?

ios - 来自数据库的 Realm 部分日期?

iOS - Swift - UIImage 上的可点击区域

ios - 如何在iPhone SDK中使用手势在屏幕上绘图?

ios - UITableView heightForHeaderInSection 不工作

ios - 另一个实体的核心数据过滤器

ios - Error Domain=NSURLErrorDomain Code=-1005 "网络连接丢失

objective-c - 为 UITableView 的每个部分添加单独的标题

ios - NSPredicate 获取给定值下属性的最大值

iphone - iOS RestKit + 核心数据。获取缓慢且核心数据错误