1

I have a view controller where you can change your gender. I have a tableview loaded with an array that just has two strings, Male or Female.

If the user was originally set as female, then there's a checkmark on the Female cell. Then when you tap the Male cell, the Male cell should get checked and the female cell should get unchecked.

My logic is almost working, but when I first tap on an unchecked cell (Male cell), the checked cell doesn't uncheck (so both Male and Female are checked). But then after I select a cell again, it works as desired.

How do I fix this bug... it only occurs the first time I tap on an unchecked cell, then works normally.

I went through a lot of SO links but still getting the same result.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"genderCell"];
    cell.textLabel.text = self.genderArray[indexPath.row];

    if (([UserParseHelper currentUser].gender = @"Female"))
    {
        if (indexPath.row == 1)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    else if (([UserParseHelper currentUser].gender = @"Male"))
    {
        if (indexPath.row == 0)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    return cell;
}

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

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

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

enter image description here

May Yang
  • 523
  • 1
  • 5
  • 18

1 Answers1

0

All goes same just Change gender of current user in didSelectRowAtIndexPath method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
        [UserParseHelper currentUser].gender = @"Male";
    else if(indexPath.row==1)
        [UserParseHelper currentUser].gender = @"Female";
    [tableView reloadData];
}

No need of didDeselectRowAtIndexPath method

/*-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}*/
Viral Savaj
  • 3,379
  • 1
  • 26
  • 39