Kjell
Kjell

Reputation: 781

UITableViewCell checkmark toggle not working

I'm working with a UITableViewCell.

When I tap it, it does toggle the first time between: UITableViewCellAccessoryNone & UITableViewCellAccessoryCheckmark

When I tap it, I even get the NSLogs that show that the toggle should be working properly.

But visually, only the first time I tap the cell does it actually toggle. Any ideas why?

If I close and reopen the viewcontroller, the checkmark/absence of checkmark is displayed properly (I have these saved), and once again I can only toggle once.

Any help please?

Here's my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
int i = indexPath.row;

cell.textLabel.text = [[savedData.packArray objectAtIndex:i] name];
if ([[[savedData packArray] objectAtIndex:i] owned])
{
    cell.detailTextLabel.text = @"Owned";
}
else
{
    cell.detailTextLabel.text = @"$0.99";
}

if ([[savedData.packArray objectAtIndex:i] owned])
{
    if ([[savedData.packArray objectAtIndex:i] activated])
    {
        [[savedData.packArray objectAtIndex:i] setActivated:NO];
        cell.accessoryType = UITableViewCellAccessoryNone;
        NSLog(@"No checkmark");
        //[cell setSelected:NO];
    }
    else
    {
        [[savedData.packArray objectAtIndex:i] setActivated:YES];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        NSLog(@"Yes checkmark");
        //[cell setSelected:YES];
    }
    [self saveData];
}
else
{
    NSString *msg = [NSString stringWithFormat:@"Unlock %@?", [[savedData.packArray objectAtIndex:i] name]];
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Unlock for $0.99?" message:msg delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Purchase", nil];
    [av show];
    //[cell setSelected:NO];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

[self saveData];
//[cell setSelected:NO];
//return indexPath;
}

Upvotes: 1

Views: 213

Answers (2)

Dino Tw
Dino Tw

Reputation: 3321

After you tap the table cell, not only the UI, but you will also need to update the value of activated in savedData.packArray.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318944

Your code to get the cell in your didSelectRowAtIndexPath method is incorrect. You want this:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Why do you set the cell's text when the cell is selected? This should be done in the cellForRowAtIndexPath data source method. You should only be toggling the checkmark in the didSelectRow method.

Upvotes: 3

Related Questions