0

I am using the scrollToRowAtIndexPath to center my tableview on the previously selected cell, however its crashing the app while testing on the phone but not on the emulator.

This is how I set it up

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //Center previously selected cell to center of the screen
    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
} 

And this is the error its giving

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (0) beyond bounds (0).'
*** First throw call stack:
(0x31fe88bf 0x31d581e5 0x31fe87b9 0x31fe87db 0x32b6df1f 0x29bef 0x32ae26b5 0x32b3daf1 0x32afed21 0x32afea71 0x32afe78b 0x32afe4ff 0x32ab581b 0x32abafb9 0x3193aba7 0x3273be8d 0x31fbb2dd 0x31f3e4dd 0x31f3e3a5 0x3204afed 0x32ace743 0x34f5 0x2f70)
terminate called throwing an exception(gdb) 

The thing being is I set

[self.tableView reloadData];
        [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

in a different method the application works fine but dose not scroll to the index position.. I dont know how to fix it.

UPdate New error when trying to delay scrollToRowAtIndexPath

2011-11-02 16:00:58.180 code[2303:707] -[VehicleResultViewController methodThatCallsScrollToRow]: unrecognized selector sent to instance 0x1f3410
2011-11-02 16:00:58.184 code[2303:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[VehicleResultViewController methodThatCallsScrollToRow]: unrecognized selector sent to instance 0x1f3410'
*** First throw call stack:
(0x31fe88bf 0x31d581e5 0x31febacb 0x31fea945 0x31f45680 0x35bef943 0x31fbca63 0x31fbc6c9 0x31fbb29f 0x31f3e4dd 0x31f3e3a5 0x3204afed 0x32ace743 0x34a9 0x2f24)
terminate called throwing an exception(gdb) 
C.Johns
  • 10,185
  • 20
  • 102
  • 156

1 Answers1

4

You probably don't need to reload data - just run your scrollToRowAtIndexPath after a delay:

[self performSelector:@selector(methodThatCallsScrollToRow) withObject:nil afterDelay:2.5];

Give the table a chance to load.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • weird.. I have no idea why this is happening but after putting that line of code in I started getting error I have added above every time I try to access the subview. – C.Johns Nov 02 '11 at 03:02
  • lol bloody hell! lol **methodThatCallsScrollToRow** lol.. thats whats c ausing the error.. the thing being is that the scroll code is inside viewDidAppear method – C.Johns Nov 02 '11 at 03:04
  • AWESOME! worked perfectly! you my friend deserve a high five! lol taken me all day to track the actual problem down.. and this solved it nicely I dropped the time down to 0.5 and it animates nicely. I think I will add a small loading thingy to get this really looking right for the user.... very cool thanks – C.Johns Nov 02 '11 at 03:12
  • 1
    Seems very hackish and could cause a race condition between the table reload. This seems to be the 'better' way of doing this: http://stackoverflow.com/a/16071589/82976 – Jay Q. Dec 05 '14 at 01:53
  • @JayQ. The method given above is functionally the same as the second method (dispatch_async) and was written prior to that answer. – Owen Hartnett Dec 08 '14 at 18:56
  • @owen Specifying a delay is different from waiting for the main thread. performSelector works differently from GCD. – Jay Q. Dec 08 '14 at 22:14
  • @JayQ. The delay is arbitrary, could be 0.0. At any rate, your main thread will get called. performSelector is functionally identical to dispatch_async. – Owen Hartnett Dec 08 '14 at 22:36
  • Hi @JayQ - that's comparing dispatch_sync to performSelectorOnMainThread. I'm comparing dispatch_async to performSelector. There may be some slight differences in Apple's implementation, but it should provide the same result. – Owen Hartnett Dec 09 '14 at 19:58