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

chhoda

macrumors 6502
Original poster
Oct 25, 2008
285
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
 
is that feature broken somehow ?
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];
 
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
 
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?
 
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?


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.
 
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;
 
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;

How exactly would you go about doing this? (Sample code please)

Thanks a bunch
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.