gpt4 book ai didi

ios - 从 data core ios 获取数据

转载 作者:行者123 更新时间:2023-11-29 02:43:07 25 4
gpt4 key购买 nike

我正在开发 ios 应用程序,当单击添加按钮时,它会将我带到另一个 View Controller 以填充我的信息并将它们保存在核心数据中如何获取此数据并将其发布到 Tableview 中有什么帮助吗?

最佳答案

保存数据(在您获取数据的 View Controller 中)

在你的.m文件中添加

- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}

并在您的保存操作中添加

- (IBAction)savePressed:(id)sender
{
NSManagedObjectContext *context = [self managedObjectContext];

NSManagedObject *newData = [NSEntityDescription insertNewObjectForEntityForName:@"StoredData" inManagedObjectContext:context];

[newData setValue:textField.text forKey:@"dataattribute"];

NSError *error = nil;

if (![context save:&error]) {
NSLog(@"Save Failed! %@ %@", error, [error localizedDescription]);
}

}

在表格 View Controller 中

- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}

在你看来确实出现了

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"entityName"];
arrayData = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];


[self.tableView reloadData];
}

在你的 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSManagedObject *data = [arrayData objectAtIndex:indexPath.row];

[cell.textLabel setText:[NSString stringWithFormat:@"%@", [data valueForKey:@"dataattribute"]]];

return cell;
}

关于ios - 从 data core ios 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25502010/

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