Mikkel V.
Mikkel V.

Reputation: 155

Xcode: Tableview checkmarks

I wanna know how to check some cells in a table view. Does somebody know how to do that? I want, when the user presses a cell then a checkmark will appear and if the user presses again it ill disappear agian. The user should be available to check more than 1 cell at a time ;)

Upvotes: 0

Views: 2768

Answers (1)

rishi
rishi

Reputation: 11839

you can set accessoryType property of UITableViewCell to UITableViewCellAccessoryCheckmark. You just need to keep a BOOL variable that will let you allow to change between UITableViewCellAccessoryNone and UITableViewCellAccessoryCheckmark.

EDIT 1 -

Code for doing so-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *tableCell = [tableView cellForRowAtIndexPath:indexPath];
    BOOL isSelected = (tableCell.accessoryType == UITableViewCellAccessoryCheckmark);

    if (isSelected) {
        tableCell.accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

Upvotes: 1

Related Questions