I use UITableView to select one (many) elements. Like the UI when choosing a ringtone, I want the selected item to be checked, and others not. I would like the cell to be selected when touched and then animated back to its normal color (again, like the user interface for selecting ringtones).
The subclass of UIViewController is my table delegate and data source (not a UITableViewController, because I also have a toolbar).
I install a cell type accessory in cellForRowAtIndexPath :, and update my model when cells are selected in the didSelectRowAtIndexPath: file. The only way I can set the selected cell to check (and clear the previous one) is to call [tableView reloadData] in the didSelectRowAtIndexPath: file. However, when I do this, the cell selection animation is strange (a white square appears where the cell label should be). Unless I call reloadData, of course, the accessoriesType type will not change, so no checkmarks will appear.
I suppose I could turn off the animation, but it seems lame. I also played with getting and modifying the cells in the didSelectRowAtIndexPath: file, but this is a big pain.
Any ideas? Short code follows ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kImageCell]; if( aCell == nil ) { aCell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kImageCell]; } aCell.text = [imageNames objectAtIndex:[indexPath row]]; if( [indexPath row] == selectedImage ) { aCell.accessoryType = UITableViewCellAccessoryCheckmark; } else { aCell.accessoryType = UITableViewCellAccessoryNone; } return aCell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; selectedImage = [indexPath row] [tableView reloadData]; }
source share