2012-02-04 100 views
0

即时尝试实现与每个单元格上的复选框希望状态(选中/未选中)的表视图将保存在CoreData数据库上。目前的实施仍然没有成功。如果我能得到一些帮助,我会非常感谢。CoreData与UITableViewCell中的复选框

有问题的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [pictureListData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
    // Get the core data object we need to use to populate this table cell 
    Compras *currentCell = [pictureListData objectAtIndex:indexPath.row]; 

    BOOL checked = currentCell.status; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"[email protected]"] : [UIImage imageNamed:@"[email protected]"]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    button.frame = frame; // match the button's size with the image size 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet 
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.accessoryView = button; 


     return cell; 
} 

- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil) 
    { 
     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 

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

    Compras *currentCell = [pictureListData objectAtIndex:indexPath.row]; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIButton *button = (UIButton *)cell.accessoryView; 
    UIImage *image = [UIImage imageNamed:@"[email protected]"]; 

    // patient.check is a string value set to either "Y" or "N" 
    // This allows the check mark to be permanently saved 
    if (currentCell.status) { 
     image = [UIImage imageNamed:@"[email protected]"]; 
     currentCell.status = NO; 
    } 
    else { 
     // image defaults to checked.png 
     image = [UIImage imageNamed:@"[email protected]"]; 
     currentCell.status = YES; 
    } 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 

} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Get a reference to the table item in our data array 
     Compras *itemToDelete = [self.pictureListData objectAtIndex:indexPath.row]; 

     // Delete the item in Core Data 
     [self.managedObjectContext deleteObject:itemToDelete]; 

     // Remove the item from our array 
     [pictureListData removeObjectAtIndex:indexPath.row]; 

     // Commit the deletion in core data 
     NSError *error; 
     if (![self.managedObjectContext save:&error]) 
      NSLog(@"Failed to delete picture item with error: %@", [error domain]); 

     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+0

'currentCell'似乎是一个'NSManagedObject',对吧?它的'status'属性的类型真的是'BOOL'还是'NSNumber'和'BOOL'值? – Costique 2012-02-04 13:01:53

+0

是的,它的BOOL :) – Bruno 2012-02-04 13:04:41

回答

3

您在评论中写道,这currentCell是NSManagedObject。 由于CoreData只能处理对象,布尔值在NSNumber的

实例包裹
BOOL checked = [currentCell.status boolValue]; 

您的代码将总是导致YES,因为你正在写的对象的地址为BOOL。它很可能永远不会是0.