Quick post about a problem I had today.
I’m writing an app for a client that has a checklist component. So a UITableViewCell is in an unchecked state, and then when the user hits it, it becomes checked.
I like the look of the UITableViewCellAccessoryCheckmark, so I’m using it, but I needed something when the cell is unchecked, so I’m using a png that has a circle.
I was having a problem that when the cell was unchecked and I tapped on it, nothing appeared to happen, although if I went to another screen and came back to that one the check box would show up. But, if the cell was checked and I tapped on it, the circle would show up. Here’s the code that fixed it:
//Without this, the accessory view will trump the
//FIXME: - Alloc during scrolling is slow - this is slow if there are many rows 7
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.uncheckedImage ]; 8 cell.accessoryView = imageView ; 9
[imageView release ]; 10}
I had forgotten cell.accessoryView =nil; and without doing that, the accessoryType was getting set, but the accessoryView was still there, so the accessoryType never got a chance to show up.
Note that in this case, I’m allocing a new UIImageView for every row. As I’ve commented here, you shouldn’t do this if you have a lot of rows and scroll speed is important. This app has maybe 15 rows at a time, so I’m not worried about performance (but I might still go and reuse a pre-alloc’ed array of ImageViews just because it will bug me if I don’t).