Stangs55
Stangs55

Reputation: 41

Multiple Taps Required in UITableView for Checkmarks

Be gentle, I'm learning.

The code below actually works fine except for the fact that after I tap a row for the first time (which appropriately places a checkmark), I then have to tap a cell twice to get it to remove that same checkmark.

Similarly, after removing it, it requires two taps to put the checkmark back.

How do I fix this?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    NSInteger selectedRow = indexPath.row;

    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
        if (selectedRow == 0) dm = 1;
        else if (selectedRow == 1) athero1 = 1;
        else if (selectedRow == 2) athero2 = 1;
        else if (selectedRow == 3) athero3 = 1;
        else if (selectedRow == 4) familyHistory1 = 1;
        else if (selectedRow == 5) familyHistory2 = 1;
    }
    else  {
        thisCell.accessoryType = UITableViewCellAccessoryNone;
        if (selectedRow == 0) dm = 0;
        else if (selectedRow == 1) athero1 = 0;
        else if (selectedRow == 2) athero2 = 0;
        else if (selectedRow == 3) athero3 = 0;
        else if (selectedRow == 4) familyHistory1 = 0;
        else if (selectedRow == 5) familyHistory2 = 0;
    }
}

Upvotes: 2

Views: 867

Answers (3)

user1623433
user1623433

Reputation: 21

make sure allowsMultipleSelection is not set to YES. The default value set when this property is not used is NO. In case if you are using this property anywhere in your class then try setting it to NO to check if it works. This will cause requiring 2 taps for selecting a row.

self.tableView.allowsMultipleSelection = NO;

Upvotes: 2

Nina
Nina

Reputation: 1679

Have an array to store the selected row indices. In -didSelect, check whether the row index already present in the array, if not add it, else remove it from the array. And reload table. Thats all. I believe this would work. :]

Upvotes: 1

meriial
meriial

Reputation: 5136

Is it possible that you are pushing two UITableViewControllers onto your navigation controller stack?

I had this happen to me, and the problem was a missing 'break' in a switch statement that caused a second TableViewController to get pushed. Everything on the view required two taps, even my 'Back' button.

Upvotes: 0

Related Questions