Tanvir Nayem
Tanvir Nayem

Reputation: 722

How to deselect tableview cell when selecting another tableview cell in the meantime?

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

Answers (2)

Nirmalsinh Rathod
Nirmalsinh Rathod

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

rmaddy
rmaddy

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

Related Questions