cell check mark broken ?

Discussion in 'iOS Programming' started by chhoda, Sep 27, 2009.

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #1
    Hi All,

    its funny,

    i wrote following code for check and uncheck cells in my uitableview

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

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{
    thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
    }


    but the check on/off does not seem to work. I have written a custom cell and have added a label and textbox added and laid out in its layoutsubviews function. I disabled the codes for these controls to see if the check mark is being hidden by them, it seems the cell never shows check mark. is that feature broken somehow ?

    ch
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Shouldn't be. I just tested it with a simple project and the checkmark is appearing and disappearing as necessary.

    But wait! If you've subclassed UITableViewCell shouldn't you use this line instead?:
    Code:
    [I][B]MyCustomTableCell[/B][/I] *thisCell = [tableView cellForRowAtIndexPath:indexPath];
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Do you also set the accessoryType in cellForRowAtIndexPath?
     
  4. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #4
    yes but

    thanks for the replies. yes i am using my customcell class, here in the forum i typed the wrong name. But I am still not successful. does the check box show up right side of the cell ? could it be that the laying out subviews code i add in customcell layoutSubviews method are hiding the checkbox ?

    it is funny, when i create all the table cells upfront and keep it an array and pick them up. checkbox displays. But when i use reuse identifier it does not !

    CH
     
  5. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #5
    Also, if I understand the TableView concept correctly, you need to save the checkmark setting somewhere else. If your cell gets scrolled out of view the TableViewController might release the cell. Then, when you're asked to re-create the cell in your delegate, you have to restore it in the same state as before.

    At least that's how I understand it. In the beginning I thought I could just layout the controls in the table cells, let the user edit the values, and when the user clicks "Save" I can simply pick up the edited values from the cells. But I assume that's very wrong, I need to pick up the changes and save the values as soon as they're made by the user. Can anyone confirm that?

    The documentation is not very clear on exactly what happens if you pass nil for reuseIdentifier when creating a TableViewCell. Is you set the reuseIdentifier to nil the cell will clearly never be reused, but does this mean that it will also never be released by the table view? Or can it still be released?
     
  6. Niiro13 macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #6

    That's about right. Apple does allow for like one or two cells off screen (meaning scroll offset) before releasing them. So if your cell only goes off screen for like half a cell's height then it should still be there.

    For the reuse identifier, you can pass in nil and the cell will be released as usual. The difference is that memory will need to be allocated every single time the cell needs to be displayed again as opposed to being able to just grab an old cell and changing the properties. Alloc-Init is very expensive.
     
  7. mraheel macrumors regular

    Joined:
    Apr 18, 2009
    #7
    Are you trying to have Multiple checked cells?
    you've not kinda described how these checked cells are behaving, if i understand it correctly.. they're not retaining the checked status and getting jumbled up cuz of the reuse issue.

    The way i did it was to add a BOOL to my customcell class. say, cell.Checked. And Make an alternative data source for those "Checked" cells.

    You'll need to tweek didSelectRow and CellForRowAtIndexPath,

    didSelectRow is to change the status from Checked/Unchecked. And cellforrow is for scroll.

    in DidSelectRow, Check if the Cell is checked.
    Code:
    If (cell.Checked) {
    //Uncheck Cell
    }else { check cell 
    }
    in CellForRowAtIndexPath
    Heres where the datasource comes into play, You need something to check if the cell was checked and prevent reuse cell complication where upon scroll other cells get checked.
    verify if cell object is present in datasource, if so, cell.checked = YES else, cell.checked = NO;
     
  8. trojanvillage macrumors member

    Joined:
    Jan 1, 2008
    Location:
    Toronto
    #8
    How exactly would you go about doing this? (Sample code please)

    Thanks a bunch
     

Share This Page