我有一个带有获取数据的自定义tableViewCell和一个篮子按钮,如何通过单击该单元格中的篮子将它们保存到另一个实体(篮子实体)?
- (IBAction)basketButton:(id)sender
{
NSManagedObjectContext *context = [app managedObjectContext];
UIButton *button = sender;
NSInteger rowInIndexPath =button.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowInIndexPath inSection:0];
Basket *basket = [NSEntityDescription insertNewObjectForEntityForName:@"Basket" inManagedObjectContext:context];
//missing code
NSError *error;
if (![context save:&error])
{
NSLog(@"fail : %@", [error localizedDescription]);
}
}
我的解决方案:
我忘了告诉我的功能Cell的位置。
- (IBAction)basketButton:(id)sender {
NSManagedObjectContext *context = [app managedObjectContext];
UIButton *button = sender;
NSInteger rowInIndexPath =button.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowInIndexPath inSection:0];
Basket *basket = [NSEntityDescription insertNewObjectForEntityForName:@"Basket" inManagedObjectContext:context];
Product *product = [self.fetchedResultsController objectAtIndexPath:indexPath];
basket.productname =product.productname ;
NSError *error;
if (![context save:&error])
{
NSLog(@"fail : %@", [error localizedDescription]);
}
}
答案 0 :(得分:0)
尝试learning more关于他们。查看https://stackoverflow.com/questions/51971/can-anyone-recommend-a-complete-objc-cocoa-or-cocoa-touch-tutorial的答案,了解大量资源,帮助您入门。