Search code examples
iosipaduitableviewuipopovercontroller

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


Solution

  • 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.