我想知道什么是最好的方法:在编辑模式下创建一个NSManagedObject。我在想
[NSEntityDescription insertNewObjectForEntityForName:nil inManagedObjectContext:managedObjectContext];
[self setEditing:YES animated:YES];
// Start editing the new object name.
WVGenericTableViewCell *cell = (WVGenericTableViewCell *)[self.tableView cellForRowAtIndexPath:[self.fetchedResultsController objectAtIndexPath:indexPath]];
[cell makeNameFieldFirstResponder];上述情况给我带来了以下问题:
1.单元格不becomeFirstResponder 2. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField使我的应用程序崩溃
看上去像这样
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//Get IndexPath From TextField
WVGenericTableViewCell *cell =(WVGenericTableViewCell *) textField.superview.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Ingredient *newIngredient = [self.fetchedResultsController objectAtIndexPath:indexPath];
newIngredient.name = textField.text;
//Save
[[self coreDataStack] saveManagedObjectContext];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}如果有更好、更优雅的方法,任何建议或替代方案都将受到高度赞赏,请指出
编辑:好
[NSEntityDescription insertNewObjectForEntityForName:nil inManagedObjectContext:managedObjectContext];insertNewObjectForEntityForName不可能是零(崩溃问题解决了)
发布于 2014-03-20 05:22:00
更好的方法是创建一个具有“主队列”并发类型的新NSManagedObjectContext,并在其中执行对新创建或现有项的所有更改(通过将它们导入新上下文来编辑现有项)。
这样,您的主上下文仍然可以保存对其的更改,而无需提交更改--仅部分填充的新对象。
而且,您可以简单地丢弃新上下文而不保存它,从而丢弃所有更改。
上下文可以是主上下文的子上下文,也可以通过通知报告更改。
https://stackoverflow.com/questions/22521959
复制相似问题