开发者

Deselect row in UITableView which is a subview

I h开发者_运维问答ave a UITableView which is added to a view as a subview. When selecting a row a new view will be present with pushViewController: and the user has the option to push a Back-button to go back to the UITableView but then the cell is still selected.

Is there a way to deselect this when the view appears?

I have read that I should use the following code but I can't get it working. No errors, no warnings, no nothing.

[tableProgram deselectRowAtIndexPath:[tableProgram indexPathForSelectedRow] animated:NO];


You should deselect the row in the didSelectRowAtIndextPath method of the delegate of your UITableView. It should look something like this.

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

    /* initialize your view controller here v and then push it */
    SomeViewController *v = [[[SomeViewController alloc] init] autorelease];
    [self.navigationController pushViewController:v animated:YES];
}

Dot confuse it with didDeselectRowAtIndextPath method - it happens as people do not pay much attention when selecting methods from intelisense.


Another place you can use this is inside viewDidAppear

- (void)viewDidAppear:(BOOL)animated

and insert the following

NSIndexPath *indexPath = [tableView indexPathForSelectedRow];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

The cell stays selected after clicked and pushed to a different view controller, but deselects when it pops back to the original view controller, so it gives the user a visual cue for the last selected row.


quote SimonBS:

tried that and hoped it would work (to get the animation) but it seems that both viewDidAppear:(BOOL)animated and viewWillAppear:(BOOL)animated aren't called when in a subview.

the code by honcheng does work

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSIndexPath *indexPath = [self.tableViewOutlet indexPathForSelectedRow];

    [self.tableViewOutlet deselectRowAtIndexPath:indexPath animated:YES];

}

you just need to use it on the TableView's superview. (For instance: if you have a xib file with a TableView subview inside the view you just need to go to the corresponding view controller code and override viewWillAppear, i just did it and it works! hello visual cue!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜