2

I have a table view with numerous cells. When I press one, a tick appears beside it, but when I select another, the tick on the previous one remains, and a new tick is added on the current cell. so two cells are ticked, but only one at a time must be ticked!!

I tried this, but it does not work:

if (cell.selected = YES) {

[cell setSelected:NO animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryNone];

}else if(cell.selected = NO){

    [cell setSelected: YES animated:YES];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
Alessandro
  • 4,000
  • 12
  • 63
  • 131

4 Answers4

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

int newRow = [indexPath row];
int oldRow = [lastIndexPath row];

if (newRow != oldRow)
{
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                                                indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

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

        lastIndexPath = indexPath;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

OR

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
Mani
  • 1,841
  • 15
  • 29
  • the second one works, but the old cell is not deselected by pressing on the new one, just if you press it twice. the firs one is merely ok. The cell becomes blue and then white again, but on the old cell the tick remains!! – Alessandro Jun 09 '12 at 12:10
  • I had to add the line: "the lastindex is property(retain) NSIndexPath* lastIndexPath" from Suhaiyl's answer, so thanks to him as well – Alessandro Jun 09 '12 at 12:21
  • I have spotted a problem though. if the table is long, sometimes some cells remain selected. why? – Alessandro Jun 09 '12 at 12:46
1

An alternative:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (int i = 0; i<yourArray.count;i++)
    {
        if(i!=[indexPath row])
        {
            cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

This physically goes through all of the rows and disables them. Kind of brute force, but it works. If you want a version that doesn't allow deselecting,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (int i = 0; i<yourArray.count;i++)
    {
        cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cwRichardKim
  • 1,030
  • 1
  • 9
  • 15
1

The quick fix in Swift:

 var lastIndexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let newRow = indexPath.row
    let oldRow = lastIndexPath.row

    if oldRow != newRow {
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
        tableView.cellForRowAtIndexPath(lastIndexPath)?.accessoryType = .None

        lastIndexPath = indexPath
    }

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Rob Norback
  • 6,401
  • 2
  • 34
  • 38
0

In the tableviewdatasource you need to follow this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil )
    {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.lastIndexPath = indexPath;

    [tableView reloadData];
}

the lastindex is property(retain) NSIndexPath* lastIndexPath;

Suhaiyl
  • 1,071
  • 9
  • 17
  • instead of [cell setSelected:NO animated:YES]; did you try with [tableView deselectRowAtIndexPath:indexPath animated:YES]; ?? – Suhaiyl Jun 09 '12 at 12:17
  • Hey just have a look at this link : http://stackoverflow.com/questions/1750753/uitableviewcell-accessory-type-checked-on-tap-set-other-unchecked hope this helps you out. – Suhaiyl Jun 09 '12 at 12:26