mr. knott
mr. knott

Reputation: 61

didDeselectRowAtIndexPath issue

I have a tableview in a scrollview in a popover. When the view is presented, the bottom cell in tableview is not visible to the user. If I select all of the cells then deselect the fist cell, the out of view cell is deselected too. Has anyone come across this behaviour before? If so, how to approach it?

Upvotes: 0

Views: 87

Answers (3)

shankar
shankar

Reputation: 76

Now your job is to find all the visible cells in the tableview and then apply select/deselect to it.

UITableView *tableView = self.tableView; 
// Or however you get your table view

NSArray *paths = [tableView indexPathsForVisibleRows];

//  For getting the cells themselves

NSMutableSet *visibleCells = [[NSMutableSet alloc] init];

for (NSIndexPath *path in paths) 
{
[visibleCells addObject:[tableView cellForRowAtIndexPath:path]];
}

// Now visibleCells contains all of the cells you care about.

Upvotes: 1

Tim Bodeit
Tim Bodeit

Reputation: 9693

If you are using dequeueReusableCellWithIdentifier: change your cellForRowAtIndexPath: to use dequeueReusableCellWithIdentifier:forIndexPath:

In a UITableView cells get reused. That means it only produces as many as absolutely needed. As soon as a new one is coming onto the screen, the last one is "recycled" instead of initialising a whole new instance.

This makes your application run faster. It also means that you have to undo any changes you made, when recycling.

Selection status is one of them. The UITableView should manage this automatically for you, if it is dequeued with the relevant indexPath. If not, it wouldn't know whether that specific cell should be selected.

Upvotes: 0

Jay Bhalani
Jay Bhalani

Reputation: 4171

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

- (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: 0

Related Questions