Bruno
Bruno

Reputation: 1032

CoreData with a CheckBox in a UITableViewCell

im trying to implement a tableview with a checkbox on each cell in wish the status(Checked/Unchecked) will get saved on a CoreData data base. i still had no success with the current implementation. If i could get some help i would be very Thankful.

The code in question:

- (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];
    }   
}

Upvotes: 0

Views: 572

Answers (1)

vikingosegundo
vikingosegundo

Reputation: 52227

You wrote in a comment, that currentCell is a NSManagedObject. As CoreData only can deal with objects, bool values are wrapped with in an instance of NSNumber

BOOL checked = [currentCell.status boolValue]; 

your code will always result in YES, as you are writing the object's address to the BOOL. and it will most likely never be 0.

Upvotes: 3

Related Questions