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];
}
How to deselect a UITableViewCell using clearsSelectionOnViewWillAppear. If you set this property to be true the user's selected cell will automatically be deselected when they return to the table view.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
Deselects the row at the specified index if it's selected.
-(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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With