iCoder4777
iCoder4777

Reputation: 1692

Default Checklist in UITableView

I want to implement Checklist for a single selection in a UITableView. Also, I need a cell to be selected by default. Here's my implementation in cellForRowAtIndexPath:

NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];    
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

if (indexPath.row == selectedRow ) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
 }

didSelectRowAtIndexPath has this code:

if (!self.lastIndexPath) {
    self.lastIndexPath = indexPath;
}

if ([self.lastIndexPath row] != [indexPath row])
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    self.lastIndexPath = indexPath;  
}
else {
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

With this code I'm able to get a default checkmark, but whenever I select another row the first one remains selected until I don't click that cell. So, If I want to select my desired result what should I do?

`

Upvotes: 0

Views: 1154

Answers (1)

Craig Otis
Craig Otis

Reputation: 32054

I think the code is a little too complicated. All you need is a single property:

NSInteger _selectedRow;

This, when initially defined, will provide a default selected row. And it will also maintain the previous selection (when looking for the cell to 'unselect'):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_IDENTIFIER];
    }

    if ([indexPath row] == _selectedRow) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectedRow >= 0) {
        [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedRow inSection:0]].accessoryType = UITableViewCellAccessoryNone;
    }
    _selectedRow = [indexPath row];
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

When this view is created, if you assign:

_selectedRow = 1;

Then the second row will automatically be selected. A value of -1 indicates no default selection, and the two above methods will automatically add/remove the checkmarks from tapped rows.

Upvotes: 2

Related Questions