gpt4 book ai didi

ios - 添加 CoreData 后更新 TableView

转载 作者:行者123 更新时间:2023-11-28 22:13:14 25 4
gpt4 key购买 nike

我正在开发一个 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/

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