Here is the code in question, the button and label are set up in the
-tableView:cellForRowAtIndexPath: method
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the managedObject
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
//Get the related Item object
Item *item = [managedObject valueForKey:@"item"];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
// Get the current status and configure the checkbox button
NSNumber *status = (NSNumber *)[managedObject valueForKey:@"status"];
NSString *imageFileName = [NSString stringWithString:@""];
if ([status intValue] == 1) {
NSLog(@"status = 1");
imageFileName = [imageFileName stringByAppendingString:@"checkbox-checked.png"];
} else {
NSLog(@"status = 0");
imageFileName = [imageFileName stringByAppendingString:@"checkbox.png"];
}
UIImage *checkBoxImage = [UIImage imageNamed:imageFileName];
CGRect frame = CGRectMake(10.0f, 15.0f, 16.0f, 16.0f);
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:frame];
[button addTarget:self action:@selector(setStatusForEntry: event:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:checkBoxImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
// Add the cell's custom text label
frame = CGRectMake(40.0f, 10.0f, 200.0f, 30.0f);
UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
cellLabel.text = item.itemName;
[cell.contentView addSubview:cellLabel];
[cellLabel release];
return cell;
}
The button selector:
-(void)setStatusForEntry:(id)sender event:(id)event {
NSLog(@"setting status");
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil) {
NSLog (@"IndexPath from touches = %@", indexPath);
}
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSNumber *status = (NSNumber *)[managedObject valueForKey:@"status"];
[status intValue] == 0 ? [managedObject setValue:[NSNumber numberWithInt:1] forKey:@"status"] : [managedObject setValue:[NSNumber numberWithInt:0] forKey:@"status"];
NSLog (@"Saving new values");
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error...
NSLog (@"Saving error");
}
}
The table data is reloaded after a button click because the managedObjectContext has changed:
// NSFetchedResultsControllerDelegate method to notify the delegate that all section and object changes have been processed.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"updating table due to changed data model");
[self.tableView reloadData];
}