在CoreData中保存选定的自定义TableViewCell

时间:2012-03-20 15:16:14

标签: core-data

我有一个带有获取数据的自定义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]);
}

}

Screenshot of my app

我的解决方案:

我忘了告诉我的功能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]);
}

}

1 个答案:

答案 0 :(得分:0)

呃,从哪里开始......你在这里问的是没有任何意义的。你似乎不熟悉模型,视图,控制器,动作等基本概念......没有办法回答/解决你的问题。

尝试learning more关于他们。查看https://stackoverflow.com/questions/51971/can-anyone-recommend-a-complete-objc-cocoa-or-cocoa-touch-tutorial的答案,了解大量资源,帮助您入门。