views:

183

answers:

3

I need to scroll a tableview on initial load of my RootController. The problem is that I have to wait until cellForRowAtIndexPath completes before I can do anything using scrollToRowAtIndexPath. cellForRowAtIndexPath seems to be the last event for me to hook into after the table has loaded. Because the view has already loaded, the table is visible and thus the scrolling is visible. I need some way to make the table load, scroll and fade in or something so it doesn't just pop in.

The goal is to have the tableview sitting at say row five when the tableview initially displays. I can certainly do that now but the user sees the scroll action. The scroll action is what I'm wanting to hide on initial load of the view.

I do have a class level variable to keep track of the last index in cellForRowAtIndexPath, so I'm not firing scrollToRowAtIndexPath for each cellForRowAtIndexPath iteration.

Any other suggestions on how to do this are welcome.

A: 

I don't know if this is what you're looking for, and perhaps I don't understand your intentions, but here goes:

If it's cellForRow... you think you need to hook into, obviously this is going to be called several times and you'll have to know when it's been called the last time (the table view is going to call it for all visible rows, and possibly one or two rows outside the view frame). At the end of each call to that method, fire a delayed method invocation (perform blah blah with delay), but at the beginning of the cellForRow... method, cancel that delayed perform. This way when the method has been called the last time for the initial loading of table cells, you'll have the delayed call to your helper method which will scroll your table, etc.

I believe myTableView.hidden = YES to start would be helpful and then set to "NO" once scrolling completes. The problem is hidden = YES has no affect on the tableview. It is a connected IBOutlet.

You'd likely want to set a class property in viewWillAppear (or didAppear) to indicate whether you want the delayed scrolling action to take place, and clear that after doing your scrolling.

wkw
I've updated the question to reflect I am keeping track of cellForRow iterations. I believe I'm already accomplishing what you are suggesting with the delayed method. I don't have any issues finding the last cellForRow iteration. The only problem is hiding the scrolling action.
4thSpace
A: 

Try to override viewWillAppear in the view controller and set up your view there. This is called after your view is initialized but before it is displayed. Note that this is called every time the view is displayed (including back navigation or dismissing a modal pop-up) so if you only want to scroll the very first time the view is created you may need to use a flag, like the code sample below.

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

  if (!alreadyDisplayed) {
    // now scroll to the right row
    [self.tableView 
       scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 5 inSection: 0] 
       atScrollPosition: UITableViewScrollPositionTop animated: NO];

    alreadyDisplayed = true;
  }
}
dmercredi
+1  A: 

Hi,

Being new in this field I think this problem can be easily solved like this

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



        if(!firstTime){
         [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
        }

    }


- (void) timerFired:(NSTimer *)aTimer{
    firstTime = YES;
    [aTimer invalidate];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:0]-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

Hope this helps.

Madhup