0

我有一个表格列表,用布尔值保存在核心数据中,当我选择一个单元格时,布尔值在核心数据中保存为“是”。但是当我选择一个多于一个单元格时,有 2 个单元格为“是”。我只想将 1 个单元格设置为是。我怎样才能做到这一点。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BestuhlungCell *cell =(BestuhlungCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];


    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.bestuhlungLabel.text = [managedObject valueForKey:@"bestuhlungstitel"];
    NSData *data = [[NSData alloc] initWithData:[managedObject valueForKey:@"bestuhlungsfoto"]];
    UIImage *image = [UIImage imageWithData:data];
    cell.bestuhlungFoto.image = image;
    cell.checkButton.hidden=YES;


    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{



    NSManagedObjectContext *context = [app managedObjectContext];
  //  BestuhlungCell *cell = (BestuhlungCell *)[tableView cellForRowAtIndexPath:indexPath];
    Bestuhlung *bestuhlung=[self.fetchedResultsController objectAtIndexPath:indexPath];







    if ([[bestuhlung valueForKey:@"check"] boolValue]) {
        [bestuhlung setValue:[NSNumber numberWithBool:NO] forKey:@"check"];

    } else {

        [bestuhlung setValue:[NSNumber numberWithBool:YES] forKey:@"check"];

    }


    NSError *error = nil;
    if (![context save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }



    [self.navigationController popViewControllerAnimated:YES];


}
4

2 回答 2

0

从您的代码段中可以清楚地看出,您只是在修改选定单元格的值,而不是其他表值。

为了实现这一点,您已将表中每一行的 BOOL 值设置为 NO,然后您可以继续使用同一行代码。

于 2012-04-20T06:59:08.203 回答
0

您可以按照以下方式做一些事情:

NSArray *sections = self.fetchedResultsController.sections;
for(NSFetchedResultsSectionInfo *sectionInfo in sections) {
  for(Bestuhlung *currentBestuhlung in sectionInfo.objects {
    if(currentBestuhlung == bestuhlung) {
      // the selected bestuhlung
      if ([[bestuhlung valueForKey:@"check"] boolValue]) 
        [bestuhlung setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
      else 
        [bestuhlung setValue:[NSNumber numberWithBool:YES] forKey:@"check"];
    }
    else {
      // those are the other ones you want to uncheck
      [currentBestuhlung setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
    }
  } 
}
于 2012-04-21T00:30:25.017 回答