HurkNburkS
HurkNburkS

Reputation: 5510

UITableView select and deselect row

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

Answers (2)

Vivek
Vivek

Reputation: 5223

You need to deselect row on didSelectRowAtIndexPath method

Example code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

Upvotes: 0

nhgrif
nhgrif

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

Related Questions