selectRowAtIndexPath failing in viewWillAppear
I've got a menu that's a UITableview
in a UIPopovercontroller
that when selected scrolls the parent view's UIScollView to a specific frame.
It's working great.
The problem is if you use the pageControl to scroll the frame I need to update the selected row in the table [_delegate returnPageNumber]
returns the current pageControl.currentPage
No errors, NSLog is reporting the correct page 开发者_运维百科number:
scrollIndexPath is <NSIndexPath 0x1a3380> 2 indexes [0, 3]
But the correct cell doesn't highlight... why????
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//[tableView reloadData];
int isPage = [_delegate returnPageNumber];
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:(isPage) inSection:0];
NSLog(@"scrollIndexPath is %@",scrollIndexPath);
[tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
I've tried putting [tableView reloadData]
before and after and having the code in viewDidAppear... nothing works
try calling [super viewWillAppear]
after everything else, in viewWillAppear
:
- (void)viewWillAppear:(BOOL)animated
{
NSIndexPath *selection = [self.tableView indexPathForSelectedRow];
[[self tableView] reloadData];
if (selection)
{
[[self tableView] selectRowAtIndexPath:selection animated:NO scrollPosition:UITableViewScrollPositionNone];
}
[super viewWillAppear:animated]; //Trick is calling super last in this case. Then you can retrieve previously selected row to --> NSIndexPath *selection
}
Now working!
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[self tableView] reloadData];
int isPage = [_delegate returnPageNumber];
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:(isPage) inSection:0];
NSLog(@"viewDidAppear scrollIndexPath is %@",scrollIndexPath);
[[self tableView] selectRowAtIndexPath:scrollIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
The thing that made the difference was disconnecting the view outlet and then reconnecting it to the tableview. Not sure why this made a difference? I tried adding a view controller to the nib but it made no difference, so deleting it and reconnecting the view to the the tableview suddenly produced results. Voodoo.
精彩评论