1

I have a settings view in my app that I want the user to be able to check between some different settings but only let the user to have one of the options selected at the time.

I got some help throughout this answer: iPhone :UITableView CellAccessory Checkmark

But I'm not able to get it to toggle correctly when I have the first cell checked by default.

This is in my cellForRowAtIndexPath

if (indexPath.row == 0)
{
     // Set the first cell checked by default
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if([self.checkedIndexPath isEqual:indexPath])
{
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
     cell.accessoryType = UITableViewCellAccessoryNone;
}

This is my didSelectRowAtIndexPath

// Uncheck the previous checked row
if(self.checkedIndexPath)
{
    UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}

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

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

The cell is checked by default but when checking another cell the first cell is still checked until I check it again and after that check another cell, then the behavior is correct.

Community
  • 1
  • 1

1 Answers1

1
if (!self.checkedIndexPath && indexPath.row == 0)
{
     // save away what cell is already checked
     self.checkedIndexPath = indexPath;

     // Set the first cell checked by default
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
...

alternatively: if(!self.checkedIndexPath) self.checkedIndexPath = indexPath;

if([self.checkedIndexPath isEqual:indexPath])
{
    ...
}
else
{
    ...
}

alternate in your viewDidLoad add self.checkedIndexPath = [NSIndexPath indexPath forRow:0 inSection:0];

and then just have the checkedIndexPath = indexPath check in your cellFor...

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • after adding self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:1]; the problem is solved. Thank you @David –  Feb 27 '14 at 21:00
  • from what I can see here you probably want inSection:0, not inSection:1, but I can't see your whole app :) – David Berry Feb 27 '14 at 21:11
  • a question. When I log out self.checkedIndexPath before setting self.checkedIndexPath to [NSIndexPath indexPathForRow:0 inSection:1], the self.checkedIndexPath is 0. But if I log out self.checkedIndexPath after setting self.checkedIndexPath to [NSIndexPath indexPathForRow:0 inSection:1], the self.checkedIndexPath is -4611686018427387818. Will this cause problems when I want to set up different settings depending on which index path is chosen id my didSelectRowAtIndexPath? –  Feb 28 '14 at 07:59
  • Make sure you're logging the value as NSLog(@"%@", self.checkedIndexPath) It looks to me like you're printing it out using some other format now. – David Berry Feb 28 '14 at 16:45