views:

85

answers:

4

A view with a table gets pushed onto the screen and I want it to scroll to a certain row in the table before the screen actually displays. I use this code within the final viewcontroller.

NSIndexPath *scrollToPath = [NSIndexPath indexPathForRow:5 inSection:0]; 
[theTable scrollToRowAtIndexPath:scrollToPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

When I put it in viewDiDAppear method, then it briefly flashes from the intial position of the table (at the top) to the row I want. I don't want it to show the initial position. If I put it in viewDidLoad or viewWillAppear then it crashes with a NSRangeException, presumably because the table isn't set up yet.

How would I get it to scroll without showing the initial position?

+1  A: 

If I put it in viewDidLoad or viewWillAppear then it crashes with a NSRangeException, presumably because the table isn't set up yet.

Why isn't the table set up yet? In which method are you setting it up?

Shaggy Frog
well it gets created in cellforrowatindexpath
cannyboy
You can load the table in viewWillAppear
Shaggy Frog
A: 

I just finished wrestling with this. Mine was adding a search bar to the top of the list, initially tucked under the top... ala some of the core apps. I was actually going to ask this same question!

I fear to offer this up, as it seems those who offer things up get pounded down.. but... I was surprised that there was not an easy solution... so I ended up doing this:

I use ABTableViewCell (you can google it) for custom drawn cells (nice and fast!), and when I get called to DRAW the second row (you could do this in your customly drawn cells without ABTableViewCell), I set it there, with a single fire semaphore:

if ( self.mOnlyOnce == NO && theRow > 1 ) {
    self.mOnlyOnce = YES;
    [self.mParentTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

(choose your proper row/section, as suits you... if they were in the same section, you'd probably be setting row to something other than 0)

If you hate my solution (as I can't comment yet), please do me the favor of just leaving it at zero & letting a better solution come to the top.

Oh, also, there is an entry about hiding your search at the top... but mine was already done as a custom cell... here is that link.

enjoy

eric
+2  A: 

Thanks to Shaggy and Dying Cactus for pointing me in the right direction. The answer is to load the table and scroll in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated
{
    [theTable reloadData];
    NSIndexPath *scrollToPath = [NSIndexPath indexPathForRow:5 inSection:0]; 
    [theTable scrollToRowAtIndexPath:scrollToPath atScrollPosition:UITableViewScrollPositionTop animated:NO];   
}
cannyboy
Be sure to mark your own answer as accepted so others who read this question can see it at a glance :)
BoltClock
A: 

This thread helped me solve a similar problem. Thanks all :)

Pedro