Deselecting a UITableViewCell within a SelectRowAtIndexPath
18/05/08 16:24 Filed in: iPhone SDK
For quite a while I didn't quite understand how to
deselect a UITableViewCell from a UITableView and
yet indicate to the user a selection was made. It
goes like this:
Note: I believe this is only happening with UITableView that is created from a nib file.
So what is the big deal, just use deselectRowAtIndexPath: and it is deselect the row. Well there is a catch, or not if you are smarter than me. The catch is you need out use deselectRowAtIndexPath:indexPath animated:YES and heres why; If you set animated:NO then the row never get highlighted and the user may not really know which cell was selected. But if you use animated:YES then it deselects during the animation, right after it does the selection during the animation. Makes sense? Well not really, but the good part is that the next time the table view is shown the selection is gone, as I wanted.
Sad face gone.
Here is some code to ponder. This is a TableViewDelegate and I'm using the tableview as a modal view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do some stuff
[self.navigationController dismissModalViewControllerAnimated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- A user shows a UITableView and selects a row.
- The Row highlights to indicate the user made the selection.
- The UITableView scrolls away.
- The user shows the same UITableView again, this time the same row is selected.
- Sad Face for me the programmer
Note: I believe this is only happening with UITableView that is created from a nib file.
So what is the big deal, just use deselectRowAtIndexPath: and it is deselect the row. Well there is a catch, or not if you are smarter than me. The catch is you need out use deselectRowAtIndexPath:indexPath animated:YES and heres why; If you set animated:NO then the row never get highlighted and the user may not really know which cell was selected. But if you use animated:YES then it deselects during the animation, right after it does the selection during the animation. Makes sense? Well not really, but the good part is that the next time the table view is shown the selection is gone, as I wanted.
Sad face gone.
Here is some code to ponder. This is a TableViewDelegate and I'm using the tableview as a modal view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do some stuff
[self.navigationController dismissModalViewControllerAnimated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
|