Rhyder
Rhyder

Reputation: 101

UITableViewCell didSelectRowAtIndexPath. Deselect the "not selected" cells when I select a new one

The title says it, and I think it's pretty much a no-brainer but I can't find the answer. I think the code describes what I try to do.

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

NSLog(@"Selected Row");
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
static NSString *CellIdentifier = @"accountCell";
UITableViewCell *allCells = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
allCells.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

So - first should all cells have no checkmarks, then I wanna add a checkmark to the selected one.

Upvotes: 0

Views: 1759

Answers (2)

Sal
Sal

Reputation: 1238

See here for a possible solution. You don't need to iterate over all cells

Upvotes: 0

Josue Espinosa
Josue Espinosa

Reputation: 5089

Every time the didSelectRowAtIndexPath method is called, go through a for loop that resets all the cells to their original accessory type. Then update the cell with the selected index path with a check mark like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (UITableViewCell *cell in[self.tableView visibleCells]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

}

Upvotes: 1

Related Questions