Core data 带有文本字段和核心数据的自定义单元格
我有一个tableview,我制作了一个带有两个文本字段的自定义单元格…我希望文本字段中的文本保存在核心数据中,因此我为此创建了核心数据类,我知道如何实现核心数据方法 我无法理解如何在tableview中显示单元格,以便用户输入文本,然后将其保存在核心数据中。根据我的理解,以下是步骤: 1-默认情况下,tableview加载并显示带有文本字段的自定义单元格 2-用户输入文本,点击回车键,他得到的数据保存在核心数据中 我的问题是如何执行第1步,我无法加载视图中的数据,因为还没有数据!,我必须先显示手机 谁能帮我解决这个问题,让我明白我在这里遗漏了什么?这是我目前的代码:Core data 带有文本字段和核心数据的自定义单元格,core-data,uitableview,nsfetchedresultscontroller,Core Data,Uitableview,Nsfetchedresultscontroller,我有一个tableview,我制作了一个带有两个文本字段的自定义单元格…我希望文本字段中的文本保存在核心数据中,因此我为此创建了核心数据类,我知道如何实现核心数据方法 我无法理解如何在tableview中显示单元格,以便用户输入文本,然后将其保存在核心数据中。根据我的理解,以下是步骤: 1-默认情况下,tableview加载并显示带有文本字段的自定义单元格 2-用户输入文本,点击回车键,他得到的数据保存在核心数据中 我的问题是如何执行第1步,我无法加载视图中的数据,因为还没有数据!,我必须先显示
- (void)viewDidLoad
{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.managedObjectContext=[appDelegate managedObjectContext];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
/*
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]);
}
[self fetchedResultsController];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"machines";
cellMeios *cellM = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellM == nil) {
cellM = [[cellMeios alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cellM;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Create and configure a fetch request.
NSFetchRequest *fetchRequestMo = [[NSFetchRequest alloc] init];
NSEntityDescription *entityMo = [NSEntityDescription entityForName:@"Mo" inManagedObjectContext:self.managedObjectContext];
[fetchRequestMo setEntity:entityMo];
// Create the sort descriptors array.
NSSortDescriptor *cellTitle = [[NSSortDescriptor alloc] initWithKey:@"reference" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:cellTitle, nil];
[fetchRequestMo setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestMo managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"reference" cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = _fetchedResultsController;
return _fetchedResultsController;
}
还有其他方法,但我认为它们与我的问题无关,问题是…我需要向用户显示一些内容,以便用户可以填充
我已经有了另一个处理核心数据的tableview,但是在这种情况下,表一开始是空的,然后在tableview的顶部有一个加号按钮,然后他选择了一些内容,它被保存在核心数据中,显示了一个单元格和相关信息
提前谢谢
注意。如果您希望表格视图是可编辑的,您应该有一个类似insertNewObject的方法,当用户点击导航栏中的“+”(加号)按钮时会触发该方法。此方法的实现将创建一个新的托管对象,将其插入到上下文中,然后重新加载表。当fetchedResultsController返回为空时,也可以触发此方法 以下是我的一个视图控制器的版本:
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.managedObject managedObjectContext];
NSRelationshipDescription *relation = [[self.managedObject entity] relationshipsByName][self.relationship];
NSEntityDescription *destinationEntity = [relation destinationEntity];
NSManagedObject *newManagedObject = (NSManagedObject*)[NSEntityDescription insertNewObjectForEntityForName:[destinationEntity name] inManagedObjectContext:context];
if ([relation isOrdered]) {
[self.items insertObject:newManagedObject atIndex:[self.items count]];
}
else {
NSMutableSet *set = [self.managedObject mutableSetValueForKey:self.relationship];
[set addObject:newManagedObject];
self.items = [NSMutableOrderedSet orderedSetWithSet:set];
}
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Validation Error", @"Validation Error") message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles: nil, nil] show];
}
[self.tableView reloadData];
}
你好,史蒂文,首先感谢你回复我的帖子,我真的被这篇文章困住了。您可以这样说:“当fetchedResultsController返回为空时,您也可以触发此方法”…我如何执行此操作?我需要参考我的手机,对吗?我想做的是最好的方法?本质上,对于用户来说,要想编辑某些内容,对象必须已经存在,然后再进行编辑。此方法是在模型和表中插入新对象的示例,以便它至少有一个项。在视图控制器中实现NSFetchedResultsControllerDelegate方法controllerDidChangeContent:,该方法将在提取完成时调用。如果随后发现提取未返回任何结果,请直接调用insertNewObject。这将使用新对象重新加载表,很可能是在屏幕上显示任何内容之前。