0

I'm having a problem with the following code that should allow only one table row to be selected and display a checkmark. Currently there are multiple checkmarks being displayed in the rows.

thanks for any help

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Save the row selected to nsuserdefaults
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:cell.textLabel.text forKey:@"countrySaved"];

    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [checkmarkBtn setImage:image forState:UIControlStateNormal];
        [checkmarkBtn setFrame:CGRectMake(0, 0, 19, 19)];
        [checkmarkBtn setBackgroundColor:[UIColor clearColor]];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

       UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       [checkmarkBtn setTitle:@"" forState:UIControlStateNormal];
       [checkmarkBtn setFrame:CGRectMake(0, 0, 0, 0)];
       [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

    }

}

I saw on some posts that I have to include the following, but I am unsure about how to implement that within the code.

 cell.accessoryType = UITableViewAccessoryNone;

  if (indexPath.row == index) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
  • just reload tableview first when u click row - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Save the row selected to nsuserdefaults [aTableView reloadData]; then your code... } – Irshad Mansuri Jan 28 '13 at 14:07
  • http://stackoverflow.com/questions/5677218/only-one-uitableviewcellaccessorycheckmark-allowed-at-a-time – shahid-rasheed Jan 28 '13 at 14:13

1 Answers1

0

Just check with this code:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Save the row selected to nsuserdefaults

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:cell.textLabel.text forKey:@"countrySaved"];
    [prefs setObject:[NSNumber numberWithInt:indexPath.row] forKey:@"selectedRow"];
    [tableView reloadData];
}

In cellForRowAtIndexPath:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int val = [[prefs objectForKey:@"selectedRow"] intValue];
if (indexPath.row == val)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

    UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [checkmarkBtn setImage:image forState:UIControlStateNormal];
    [checkmarkBtn setFrame:CGRectMake(0, 0, 19, 19)];
    [checkmarkBtn setBackgroundColor:[UIColor clearColor]];
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

} else
{
    cell.accessoryType = UITableViewCellAccessoryNone;

   UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   [checkmarkBtn setTitle:@"" forState:UIControlStateNormal];
   [checkmarkBtn setFrame:CGRectMake(0, 0, 0, 0)];
   [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Please check and tell me if there is any other issues ! – Midhun MP Jan 28 '13 at 14:20
  • thanks for your post :) I'm getting a "bad receiver type" error on this line: int val = [[prefs setObject:cell.textLabel.text forKey:@"selectedRow"] intValue]; – hanumanDev Jan 28 '13 at 14:30
  • that worked great! the only thing is that when you select another row the checkmark remains on the row that was originally selected and doesn't change to the new row – hanumanDev Jan 28 '13 at 14:45
  • 1
    Why store the selected row in user defaults? And why call cellForRowAtIndexPath from the data source out to the table view to get a reference to a cell you already have in the method? – Carl Veazey Jan 28 '13 at 15:11
  • @CarlVeazey I have to store the value of the selection. this is a user prefs table. the selections/settings the user makes here changes another table view elsewhere in the app – hanumanDev Jan 28 '13 at 15:38
  • @hanumanDev: are you using the same code ? please check the val in cellForRowAtIndexPath. Checkwhether it is previous value or not ! – Midhun MP Jan 29 '13 at 04:12