Problem creating checklist (similar to TouchCells sample code) for iPhone. Random cells are checked
I am having an issue creating a TouchCells sample style checklist from Apple's sample code. Basically, it is a table that allows you to repeatedly select its elements and gives each selected element a check mark.
The problem I'm running into is that when I select an item and then scroll down the screen, a random item (off-screen) will be selected. It seems that normally the next cell is loaded onto the screen.
I couldn't figure out what I was doing wrong, so I tested it with Apple TouchCells code. However, in their program, they only have 6 cells and no scrolling space. So I duplicated some items from the plist file to make more cells and ... the same problem pops up. If you select a cell and then scroll through it, a random sample will be selected.
Update I recently tried a Cookie iPhone Dev sample code called "Checks" and ... you guessed it, the same problem. Here's the link: http://code.google.com/p/cookbooksamples/downloads/list
It drives me crazy. Is this a bug or am I doing something wrong? Does anyone know how to fix this?
Thank!
Also, does anyone know of any sample code that shows how to do this?
source to share
I have the same problem with a custom UITableViewCell in my application. According to the Apple docs on prepareForReuse : "You should only have cell reset attributes that are not content related, like alpha, edit and select."
The TouchCells example deals with selection state, but they use boolean and images to simulate selection. So far, the only thing I've found to work is to use a unique reuse ID for each cell. Kinda hits reusability goals, doesn't it?
For example, to fix the problem in the TouchCells example, replace:
static NSString *kCustomCellID = @"MyCellID";
from:
NSString *kCustomCellID = [NSString stringWithFormat:@"MyCellID%d", indexPath.row];
I think it's ok if you have a small number of cells, but there must be a better way, right?
source to share
Perhaps you can do this:
if (whatever) {
cell.accessoryType = UITableViewCellAccessoryCheckMark;
}
When you do this:
if (whatever) {
cell.accessoryType = UITableViewCellAccessoryCheckMark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
If you are using a custom cell, you can override prepareForeReuse:
- (void)prepareForReuse {
[super prepareForReuse];
self.accessoryType = UITableViewCellAccessoryNone;
}
source to share