UITableViewCell checkmark change on select
Asked Answered
I

8

54

Am I correct in thinking that to change the checkmark for "on" to "off", I must change the CellAccessoryType between none and checkmark on the didSelectRowAtIndexPath?

Because I have done this but I have noticed the behaviour is not perfectly identical to like the checkmark cells on the auto lock settings on the iphone.

Or is there some other way checkmarks are meant to be handled?

Inenarrable answered 9/5, 2010 at 9:19 Comment(0)
C
83
  1. Keep a property in your view controller called selectedRow, which represents the index of a row that represents the checked item in a table section.

  2. In your view controller's -tableView:cellForRowAtIndexPath: delegate method, set the accessoryType of the cell to UITableViewCellAccessoryCheckmark if the cell's indexPath.row equals the selectedRow value. Otherwise, set it to UITableViewCellAccessoryNone.

  3. In your view controller's -tableView:didSelectRowAtIndexPath: delegate method, set the selectedRow value to the indexPath.row that is selected, e.g.: self.selectedRow = indexPath.row

Clarino answered 9/5, 2010 at 9:51 Comment(6)
Sorry, I did not mean that I had multiple checkmark cells (more like radio buttons). Just they didn't animate as smoothly (but I fixed that now). But your answer means I am using the correct methods :)Inenarrable
To add to Alex's solution - to make the check mark appear when you tap the cell, you must also put in [tableview reloadData] after you set the selected cell.Lifeguard
That's easy, but there might be a performance hit from reloading data into the whole table. Because you know what indexPath.row you need to update, you only need to update that row, which can be done with the table view method -reloadRowsAtIndexPaths:withRowAnimation: For more details: developer.apple.com/library/ios/documentation/UIKit/Reference/…Clarino
@AlexReynolds can you please elaborate when you said set the accessoryType of the cell to UITableViewCellAccessoryCheckmark if the cell's indexPath.row equals the selectedRow value. Otherwise, set it to UITableViewCellAccessoryNone.Schism
Either the row needs a checkmark, or it doesn't. If it is the selected row (by comparing the index path row and selected row values) then mark it. Otherwise, don't.Clarino
I did this, but the checked box stays highlighted in gray instead of fading out like it does in apple apps.Utu
U
61

Another solution:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
    [self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    return indexPath;
}

And yeah: you don't have to check if oldIndex is nil :)


Swift 3 and later:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if let oldIndex = tableView.indexPathForSelectedRow {
        tableView.cellForRow(at: oldIndex)?.accessoryType = .none
    }
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

    return indexPath
}
Uptodate answered 6/4, 2013 at 8:38 Comment(1)
This is really a great code. I added the Swift version.Erleneerlewine
U
5

swift:

var selectedCell:UITableViewCell?{
    willSet{
        selectedCell?.accessoryType = .None
        newValue?.accessoryType = .Checkmark
    }
}

then:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
Underbred answered 30/1, 2016 at 23:1 Comment(0)
M
5

For swift

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
  }

  func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
  }
Merrick answered 18/1, 2019 at 21:40 Comment(0)
B
4

Zyphrax suggested a great solution that worked great for me! And if you need to clear the previous selected row, just use:

[self.tableView reloadData]; 

in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Borrell answered 9/8, 2013 at 16:42 Comment(0)
C
3

It should be

didHighlightRowAtIndexPath

instead of

tableView:didSelectRowAtIndexPath

Chasidychasing answered 30/11, 2012 at 13:17 Comment(0)
H
3

Alex answer worked for me only after adding reload table , in .h

{
  int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;

in .m *cellForRowAtIndexPath*

if(indexPath.row == selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

and in anywhere didHighlightRowAtIndexPath or didSelectRowAtIndexPath

 self.selectedCell = indexPath.row;
    [self.tableView reloadData]; 
Hole answered 25/2, 2013 at 9:45 Comment(0)
B
0

If you want to use your custom image as accessoryType use below code

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_icon.png"]];
    [imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)];
    imgCheckMark.tag = 1000+indexPath.row;

    NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow];
    [self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone;
    [self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark;
    return indexPath;
}
Babcock answered 25/6, 2015 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.