I am custom drawing cells, how can I hightlight the cell开发者_运维问答s on touchdown
thanks
The UITableViewCell has two methods that get called:
- (void) setHighlighted:(BOOL)highlighted
- (void) setSelected:(BOOL)selected
Basically setHighlighted gets called on touch down (and unset on touch up) whereas setSelected gets calls when the selection is "stuck" (the user meant to press the cell). If you play around with a normal table view and cell combo, you'll notice that the cells can highlight briefly and unhighlight when you scroll away from them.
To highlight the background, if you are using a custom background fill in drawRect you can change the color of that and call setNeedsDisplay in setHighlighted to make it force a redraw using your new background.
Use one or both of these:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
You have to provide these methods and they will be called when the row is being selected. It is up to you to change your view to make it look selected. If you haven't made to many changes to the cells, you may be able to use selectedBackgroundView for highlighting selections.
It doesn't work for a custom cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES]; - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
I used the UIImageView inside of a custom cell to animate it.
- (IBAction)highlighteMe
{
shadowImage.alpha = 0.f;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
shadowImage.alpha = 0.3f;
[UIView commitAnimations];
}
精彩评论