0

I am developing an iPhone application with UITableView. I have implemented a check mark on each cell with didSelectRowAtIndexPath delegate.

Now I want to select a cell that disable all other cells (remove the check marks) and vice versa (eg: to select 8th cell that shows the check mark on 8th cell and remove the check mark of other cells, then select other cell shows the check mark on that cell and remove the check mark on 8th cell).

How to implement this in UITableView?

halfer
  • 19,824
  • 17
  • 99
  • 186
John
  • 734
  • 3
  • 14
  • 30

1 Answers1

0

You can achieve this by adding these two ivars to your UITableViewController class to track which cell is currently checked:

NSInteger currentlyCheckedRow;
NSInteger currentlyCheckedSection;

In your initialize method, set currentlyCheckedRow and currentlyCheckedSection to a negative integer such as -1 so that it is not matched by any possible row value.

Add the following to your -tableView:cellForRowAtIndexPath: method:

// determine if cell should have checkmark
cell.accessoryType = UITableViewCellAccessoryNone;
if ( (indexPath.row == currentlyCheckedRow) &&
     (indexPath.section == currentlyCheckedSection) )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
};

Update the currentlyCheckedRow ivar when -tableView:didSelectRowAtIndexPath: is called:

 currentlyCheckedRow = indexPath.row;
 currentlyCheckedSection = indexPath.section;
 [tableView reloadData];
David M. Syzdek
  • 15,360
  • 6
  • 30
  • 40
  • Thank u for your replay.I have one question, Suppose the table view has 6 cells,i want to click the 6th cell then all other cells check will remove and click other cells (1 to 5 cells) the 6th cell check mark will remove. That means at a time 6th cell selection or 1 to 5 cells selection is possible in table view.How to implement this? – John Jun 11 '12 at 08:24