Reputation: 5510
I have a UITableViewcell that stays highlighted after touching it. I would like to know how to remove the highlight right after it becomes visible after your touch.
So when you touch the UITableViewCell I would like it to become selected and highlighted then when the user raises their finger I would like to deselect and unhighlight the UITableViewCell.
This is what I am doing so far, and the deselect works but the cell is still highlighted.
#pragma mark -- select row
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@", indexPath);
NSLog(@"%@", cell.textLabel.text);
}
#pragma mark -- deselect row
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Upvotes: 17
Views: 27899
Reputation: 5223
You need to deselect row on didSelectRowAtIndexPath
method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
Upvotes: 0
Reputation: 62072
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
That's an infinite loop, I'm quite certain. However... it's sort of on the right track. You can move that method call into didSelectRowAtIndexPath:
.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
//stuff
//as last line:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
For that matter, deselectRowAtIndexPath
can be called from anywhere at any time you want the row to be deselected.
[self.myTableView deselectRowAtIndexPath:[self.myTableView
indexPathForSelectedRow] animated: YES];
Upvotes: 34