Dancer
Dancer

Reputation: 17671

IOS UITableViewCell De-select a view once selected and returned to page

How can I deselect a cell when returning to a view?

I have an orange down state which is applied to a cell when selected - the cell navigates to a modal view when clicked - when i click back button the cell is still selected.

I have tried applying this code -

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

to my cellForRowAtIndexPath method - but it doesn't do anything!

Update - Having done a bit of research - It appears Ive missed some valuable information out of this question! - my table view is a UITableView embedded in a View Controller - not a UITableViewController - so it sounds like it doesnt have the available methods which are required for the suggestions so far..

Upvotes: 0

Views: 101

Answers (3)

ldindu
ldindu

Reputation: 4380

This is the right approach

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
   [tableView deselectRowAtIndexPath: indexPath  animated:NO]; // first line in this method

   // rest of code 

}

Upvotes: 0

Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

You should not call deselectRowAtIndexPath in cellForRowAtIndexPath method.

you can do this in viewWillAppear

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear: animated];

    NSIndexPath *selectedIndexPath = [tableViewObj indexPathForSelectedRow];
    if (selectedIndexPath != nil) {
        [tableViewObj deselectRowAtIndexPath:selectedIndexPath animated:YES];
    }
}

Or you can write in didSelectRowAtIndexPath as well

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

}

Upvotes: 1

You could use UITableViewController's clearsSelectionOnViewWillAppear property.

Upvotes: 1

Related Questions