Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
930
38
Tokyo, Japan
I have a grouped UITableView, and I used code as below from Apple to toggle check mark on when selected cell, but I found there are two cells showing check mark, I am not sure why, how can I avoid this issue?
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
 

Attachments

  • iOS Simulator Screen shot 2012.10.03 23.41.45.png
    iOS Simulator Screen shot 2012.10.03 23.41.45.png
    170.3 KB · Views: 221
  • iOS Simulator Screen shot 2012.10.03 23.41.52.png
    iOS Simulator Screen shot 2012.10.03 23.41.52.png
    149.7 KB · Views: 212
That code only toggle checkmarks on a cell that gets selected. So touching a cell will turn the checkmark on and off, but you can have as many with checkmarks as you want.

If you want only one to have a checkmark then when a cell gets selected you need to give a checkmark to that cell and then turn off the checkmark from every other cell.
 
You should keep a separate value for the currently selected row (either an integer or an indexPath). Then you can uncheck the old row and check the new row. Also, other code can tell which row is currently selected without looking at all the cells, including cellForRowAtIndexPath.
 
Maybe my question is not so clear. I need multi check marks if I touched different cells.
My question is that I just touched one cell but two cells will show check mark, when touch again, two cells will not show check mark, does it make sense?
 
I have a grouped UITableView, and I used code as below from Apple to toggle check mark on when selected cell, but I found there are two cells showing check mark, I am not sure why, how can I avoid this issue?
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

Ides has it right. I would suggest adding an NSIndexPath instance variable to your view controller. Let's call it selectedCell.

Here's how the logic might work:

If no cell is selected, set it to nil.

if the user taps a cell, see if that is the currently selected cell. If so, set its accessory view to none. (toggle the current cell to not selected).

If the selectedCell is nil, simply set the current cells accessory view to checked, and save the index path to the selectedCell.

If selectedCell is not nil, ask the table view for the cell at that index path and, if it's not nil, set it's accessory view to not checked.

No matter what the state of selectedCell, set the tapped cell's accessory view to the checked state, AND set selectedCell to the index path of the tapped cell.
 
Ides has it right. I would suggest adding an NSIndexPath instance variable to your view controller. Let's call it selectedCell.

Here's how the logic might work:

If no cell is selected, set it to nil.

if the user taps a cell, see if that is the currently selected cell. If so, set its accessory view to none. (toggle the current cell to not selected).

If the selectedCell is nil, simply set the current cells accessory view to checked, and save the index path to the selectedCell.

If selectedCell is not nil, ask the table view for the cell at that index path and, if it's not nil, set it's accessory view to not checked.

No matter what the state of selectedCell, set the tapped cell's accessory view to the checked state, AND set selectedCell to the index path of the tapped cell.
Thanks for your suggestion, can you provide a code if you don't mind?
 
Thanks for your suggestion, can you provide a code if you don't mind?

Nope. I gave you a detailed outline of what you need to do. Now you need to invest the effort to implement it yourself. If I write your code for you, you won't learn.

The second part of this is that to "provide a code" I would have to write and debug this code for you. That goes beyond providing help and guidance, and crosses the line to work for hire.
 
Last edited by a moderator:
I think one of the problems here is that UITableViews reuse cells. If a cell gets selected and has a checkmark turn on, then if the tableview is scrolled far enough that the cell goes off screen, the cell will be reused to display different content (thus conserving memory) and the checkmark will not have been turned off. This is why it may appear that more than one cell is checked even though only one was selected.
 
I think one of the problems here is that UITableViews reuse cells. If a cell gets selected and has a checkmark turn on, then if the tableview is scrolled far enough that the cell goes off screen, the cell will be reused to display different content (thus conserving memory) and the checkmark will not have been turned off. This is why it may appear that more than one cell is checked even though only one was selected.

That is likely true. One of the things you have to learn in dealing with table views is to always fully configure every cell that you return in response to tableView:cellForRowAtIndexPath: . You have to set a value for every view in the cell that you ever change, including default values (like forcing the accessory view to the "not checked" state.) That's because the cell might have been left with settings from the last time it's used.
 
I think one of the problems here is that UITableViews reuse cells. If a cell gets selected and has a checkmark turn on, then if the tableview is scrolled far enough that the cell goes off screen, the cell will be reused to display different content (thus conserving memory) and the checkmark will not have been turned off. This is why it may appear that more than one cell is checked even though only one was selected.
Thanks for your advice, I will try to fix it.
 
I think one of the problems here is that UITableViews reuse cells. If a cell gets selected and has a checkmark turn on, then if the tableview is scrolled far enough that the cell goes off screen, the cell will be reused to display different content (thus conserving memory) and the checkmark will not have been turned off. This is why it may appear that more than one cell is checked even though only one was selected.
I think there needs a array for selected cell to store its IndexPath, do you think so?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.