CoreData与UITableViewCell中的CheckBox

时间:2012-02-04 12:53:47

标签: objective-c ios cocoa-touch core-data

我试图在每个单元格上实现一个带有复选框的tableview,希望状态(Checked / Unchecked)将保存在CoreData数据库中。我目前的实施仍然没有成功。如果我能得到一些帮助,我会非常感激。

有问题的代码:

- (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:@"cb_mono_on@2x.png"] : [UIImage imageNamed:@"cb_mono_off@2x.png"];

    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:@"cb_mono_on@2x.png"];

    // 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:@"cb_mono_off@2x.png"];
        currentCell.status = NO; 
    }
    else {
        // image defaults to checked.png  
        image = [UIImage imageNamed:@"cb_mono_on@2x.png"];
        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];
    }   
}

1 个答案:

答案 0 :(得分:3)

您在评论中写道,currentCell是NSManagedObject。 由于CoreData只能处理对象,因此bool值将包含在NSNumber

的实例中
BOOL checked = [currentCell.status boolValue]; 

当您将对象的地址写入BOOL时,您的代码将始终为YES。它很可能永远不会是0。