添加CoreData后更新TableView

时间:2014-03-12 22:50:06

标签: ios uitableview core-data

我正在开发一个CoreData App,它在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];
}

1 个答案:

答案 0 :(得分:2)

您可能想要做的是使用NSFetchedResultsController

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