1

如何通过点击按钮来编辑CoreData实体中某些字段的值?(SQLite)

例如,在我的UITableViewCell我有按钮。我想要这个按钮来改变一些布尔字段的值。第一次点击我想写YES,第二次点击 - NO

1.Join是手动创建的join表成分<->>Join<<->Recipes

2._ingredientInfo 仅包含 1 条记录

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellIngredient";
    IngredientCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[IngredientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    Ingredients *ingredient = [ingredientsArray_ objectAtIndex:indexPath.row];
    cell.nameLabel.text = ingredient.name;

    //Указываем что-то типа DataSource
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = appDelegate.managedObjectContext;
    if (context != nil) {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inRecipe == %@ AND ingredient == %@", self.recipe, ingredient];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Join" inManagedObjectContext:context];
        [request setEntity:entity];
        [request setPredicate:predicate];

        NSError *error = nil;
        NSMutableArray *mutableFetchResult = [[context executeFetchRequest:request error:&error] mutableCopy];
        if (mutableFetchResult == nil) {
            NSLog(@"No fetched objects!!!");
            abort();
        }
        _ingredientInfo = [mutableFetchResult objectAtIndex:0];
    }

    cell.countLabel.text = [NSString stringWithFormat:@"%@", _ingredientInfo.count];
    if (_ingredientInfo.inCart == [NSNumber numberWithInt:0]) {
        cell.toCartBtn.imageView.image = [UIImage imageNamed:@"runTo.png"];
    }
    else {
        cell.toCartBtn.imageView.image = [UIImage imageNamed:@"inCart.png"];
    }
    cell.unitLabel.text = ingredient.units;

    return cell;
} 

当我点击按钮cell.toCartBtn

- (IBAction)toCart:(id)sender
{
    if (_ingredientInfo.inCart == [NSNumber numberWithInt:0]) {
        _ingredientInfo.inCart = [NSNumber numberWithInt:1];
    }
    else {
        _ingredientInfo.inCart = [NSNumber numberWithInt:0];
    }
    NSError *error = nil;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if ([appDelegate.managedObjectContext save:&error]) {

        [self.ingredientsTableView reloadData];
    }
    else {
        NSLog(@"Error updating");
    }

}
4

1 回答 1

0

首先获取要修改的对象

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(id == %@)", eID]];
NSError *error;

NSArray *details = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
entityName *objEntity= nil;
//error handling goes here
if([details count] > 0)
    objEntity = (entityName *)[details objectAtIndex:0];

现在在插入时更新该实体

    if(objEntity){
    club.fieldName = @"YES";
    NSError *error = nil;
    if(![self.managedObjectContext save:&error])
        NSLog(@"Error updating");
}
于 2013-02-26T04:15:06.680 回答