3

I have a UITableView with about 20 rows. I also use check mark accessory to identify the selected. The problem I face now is that the selection gets messed up when I scroll (here I am selecting multiple rows). So after scroll the selected checkmark has vanished. Can anyone help me to find a way? in didSelectRowAtIndexPath:

NSMutableDictionary *rowDict = [tableList objectAtIndex:[indexPath row]];

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

}
else
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    checkedIndexPath =  indexPath;

    NSLog(@"the checked psths are :%@",checkedIndexPath);

}

And in cellforrowatIndexpath I use

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else 
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

But this works only for single selection. What should I do for multiple selection?

3 Answers 3

1

You can have an NSMutableArray which can add n number of NSIndexPaths corresponding to the cells selected. When a cell is deselected, you can remove that indexpath from the array.

1
  • so you say to do this didSelectRowAtIndexPath and then match the array in cellForRowAtIndexPath to check or uncheck the creating cell right?
    – rashii
    Nov 24, 2011 at 5:13
0

You should use an array to store checked indices. If you use a single property then only the last selected row will get the correct accessoryType.

0

I used this code many times (With some minor changes as required)

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

       UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


        if (thisCell.accessoryType == UITableViewCellAccessoryNone) 
        {
          thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
      else
       {
          thisCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

   - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView
             accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
     {
        //add your own code to set the cell accesory type.
               return UITableViewCellAccessoryNone;
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.