Lars - Lars - - 9 months ago 42
iOS Question

Checkbox in a table

I want to add a tick box (or if checkbox is a better name) in a table to every table row.
A small simple box to mark if a row is read or not. Just to remember.
And of course to be saved when I close the app.
And also to still be unchanged if the app is updated.

Is there an easy way to do that?

Is there any samples around?

Answer Source

In the following code I am using a code for making a todo list, here I am using two NSMutableArray itsToDoTitle and itsToDoChecked to populate the table rows..I hope this will help you..

You can have the the itsToDoTitle and the itsToDoChecked Array in NSUserDefaults or you can write in some properties file so that you will have the same list again..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *CellIdentifier = @"ToDoList";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.textLabel.text = [itsToDoTitle objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    BOOL checked =  [[itsToDoChecked objectAtIndex:indexPath.row] boolValue];
    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.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.tag = indexPath.row;
    [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.accessoryView = button;

    return cell;

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

    BOOL checked = [[itsToDoChecked objectAtIndex:indexPath.row] boolValue];
    [itsToDoChecked removeObjectAtIndex:indexPath.row];
    [itsToDoChecked insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Save"
                                                                   style:UIBarButtonItemStylePlain target:self action:@selector(saveChecklist:)];
    self.navigationItem.rightBarButtonItem = backButton;
    [backButton release];