Sahil Chaudhary
Sahil Chaudhary

Reputation: 503

How to add a checkmark in a UITableViewCell

Say I have a small tableview(no scroll) and when a user taps an option, it changes some setting in the app. I used this function:

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      //some code here
}

Now I want to use UITableViewCellAccessoryCheckmark for the option that is selected, I mean it should display a checkmark in the cell the user selected (and remove checkmark from previously selected option). How do I do that?

Upvotes: 2

Views: 2126

Answers (2)

Paul.s
Paul.s

Reputation: 38728

How about

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

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

Upvotes: 8

Pfitz
Pfitz

Reputation: 7344

Just use the accessoryTypeproperty of a UITableViewCell.

@property(nonatomic) UITableViewCellAccessoryType accessoryType

Like this:

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

      //code for reusing or init a new cell goes here

      cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

Upvotes: 0

Related Questions