Reputation: 722
I an doing a table view cell auto selecting for the first time by using
[cell setSelected:YES animated:YES];
It select the cell programmatically for the first time. It works fine. Now I want to deselect that cell when i select another cell in the meantime. how can i do that. I know when i select a cell then tableview delegate method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is called and when deselect a cell then "did deselect row at index path" is called.
But the problem is i select the cell programmatically and when i select another cell then delegate method
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
doesn't call.
What should i do to solve this problem or what will be the logic to solve that problem?
Upvotes: 0
Views: 616
Reputation: 5186
You can use below method to deselect row:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You just need to stored index when it's selected. So you just need to check if you have selected indexpath then make it deselect.
Take an object of NSIndexPath:
NSIndexPath *currentSelectedPath;
Update your didSelectRow method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentSelectedPath)
{
[tableView deselectRowAtIndexPath:currentSelectedPath animated:YES];
}
currentSelectedPath = indexPath;
// Do your other stuff
}
Upvotes: 1
Reputation: 318774
Instead of calling setSelected:animated:
on the cell directly, call the selectRowAtIndexPath:animated:scrollPosition:
method of the table view to set the initial selection.
Upvotes: 2