Why two cells is checked in UITableView?

Discussion in 'iOS Programming' started by mikezang, Oct 3, 2012.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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;
        }
    }
    
     

    Attached Files:

  2. Ides macrumors member

    Joined:
    Mar 27, 2012
    #2
    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.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    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?
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    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.
     
  6. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    Thanks for your suggestion, can you provide a code if you don't mind?
     
  7. Duncan C, Oct 4, 2012
    Last edited by a moderator: Oct 4, 2012

    Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    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.
     
  8. Ides macrumors member

    Joined:
    Mar 27, 2012
    #8
    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.
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    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.
     
  10. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #10
    Thanks for your advice, I will try to fix it.
     
  11. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    I think there needs a array for selected cell to store its IndexPath, do you think so?
     

Share This Page