1

I have a tableview, i need to have a checkmark displayed everytime i select a row. (Multiple selection) My code is given below. I am also able to unselect a row.

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

if(nil == cell)
{
    cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell] autorelease];

}
cell.textLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"HelveticaNeue-Bold"] size:12];
textView.textColor = [UIColor colorWithRed:0.281 green:0.731 blue:0.8789 alpha:1];
cell.textLabel.text = [optionsArray objectAtIndex:[indexPath row]];
if (pathIndex == indexPath)
{
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
pathIndex = indexPath;
[surveytableView reloadData];
}

But i have a problem with the cells being reused. When i select a cell another cell elsewhere also gets selected. Only the checkmark (or no checkmark) is getting reused other details like the row title, etc., don't get reused. Any solutions to fix this. Thanks in advance.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
nitin k m p
  • 329
  • 2
  • 22
  • i found a solution to this from another question answered in Stack Overflow : http://stackoverflow.com/questions/6023883/uitableview-multiple-checkmark-selection Folllow this above link. Seems to be working fine for me now. – nitin k m p Jun 14 '11 at 07:33

3 Answers3

2

Add it to cellForRowAtIndexPath:

 if ([tableView.indexPathsForSelectedRows containsObject:indexPath]) {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
 } else if (![tableView.indexPathsForSelectedRows containsObject:indexPath]) {
     cell.accessoryType = UITableViewCellAccessoryNone;
 }

Worked for me

GeRyCh
  • 1,580
  • 3
  • 13
  • 23
0

At your

if (pathIndex == indexPath)

you're comparing pointers not the values of them, try

[pathIndex isEqual:indexPath]

or use

- (NSComparisonResult)compare:(NSIndexPath *)otherObject;

Next you're assigning a value to pathIndex without retaining or copying it like

pathIndex = [indexPath copy];

(of course now since you're retaining the value, before copying your new object you have to release the previous one [pathIndex release];)

Finally, no multiple selection is provided by your implementation, only single selection. You could try adding and removing NSIndexPath objects to an NSMutableArray and check against their presence in your mutable array at cellForRowAtIndexPath instead.

tsakoyan
  • 1,891
  • 14
  • 12
0

The issue is that you only do something with the accessory if the current row matches pathIndex.... so what if it is a normal cell...? You never set it back. You want...

cell.accessoryType = UITableViewCellAccessoryNone;

if ([pathIndex isEqual:indexPath])
{
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

It is good practice to reset the cells before you set any specific properties.

Simon Lee
  • 22,304
  • 4
  • 41
  • 45