0

First of all Im going to show you the error I am getting, then I will try to explain what I am doing to get this error etc

2011-11-02 10:17:57.665 code[1327:707] *** 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 0x29c9b 0x32ae26b5 0x32b3daf1 0x32afed21 0x32afea71 0x32afe78b 0x32afe4ff 0x32ab581b 0x32abafb9 0x3193aba7 0x3273be8d 0x31fbb2dd 0x31f3e4dd 0x31f3e3a5 0x3204afed 0x32ace743 0x35a1 0x301c)
terminate called throwing an exception(gdb) 

This is what I am doing to get this error. enter image description here

Any time that I enter the subview with the dataset of indexpath[0,1] for the second time the app will throw this error.

This is the code where the error happen. subview.m

- (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]; //happens on this line
}

and this is the output from that error. enter image description here

What should I be looking at.. I dont really know whats good or bad from this. Whats really frustrating is that if I go in and out of IndexPath [0,0] from the main view or any of the other ones it works fine and there is never a problem with this app..

I am hoping someone can help me, or at the very least give me an idea of what the problem might be or where.

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • myDataArray is nil is this right? – Paul.s Nov 02 '11 at 00:07
  • Your exception is telling you that the table thinks it has zero sections. – Lily Ballard Nov 02 '11 at 00:14
  • That's what your output shows above `myDataArray = (NSMutableArray *) 0x0 0 Objects` – Paul.s Nov 02 '11 at 00:14
  • right.. yea thats correct :P I think I might know the problem... I just recently changed from passing a string around to an array object with multi values I am just going to check that I am handling it correctly when I pass it over. its just weird because it works first time but not after that. – C.Johns Nov 02 '11 at 00:20
  • Sounds like you might be setting your `NSMutableArray` to nil when you don't really mean to. Can't really be sure without seeing your code. Just do a search for setting `myDataArray = nil` and make sure you mean to do it etc. – Paul.s Nov 02 '11 at 00:30
  • cool thanks, I'll let you know how I go and when/if I find the solution I will post it. – C.Johns Nov 02 '11 at 00:33
  • bugger.. what I thought it was wasn't lol... hrmm k going to do a search for myDataArray = nil. – C.Johns Nov 02 '11 at 00:45
  • at no point do I set myDataArray to nil. – C.Johns Nov 02 '11 at 00:48
  • Yeah maybe when you reload the tableView, you get the dataSource via myDataArray which is nil and will return 0 objects in numberOfRowsInSection: and the tableView is trying to scroll through an empty tableView, check it out and hope this helped. – Herz Rod Nov 02 '11 at 00:49
  • Cheers I am going to debug whats happening with myDataArray.. its being setup inside NSXMLparser and then inside parserDidEnd I am using a predicate to get the exact data I want.. further more I am then passing it off the a method I have created that sets up the table.. This works for all of the other data sets bar this one.. (well this dataset works once.. but if you try to load it again it craps out.) – C.Johns Nov 02 '11 at 01:08
  • Have you checked for uninitialized variables in your procedures? Remember that, while instance variables are initialized to zero, ordinary local variables in procedures are "initialized" to whatever garbage is in storage when the procedure is called. – Hot Licks Nov 02 '11 at 01:12
  • No I haven't this is the first time I have heard of such variables :P Is there any special way of tracking them down? – C.Johns Nov 02 '11 at 01:16
  • If i initialize my indexPath that I use to set scrollToRowAtIndexPath all of the correct data loads. I'm pretty sure myDataArray works sweet. – C.Johns Nov 02 '11 at 01:36
  • Okay guys I think I have figured out what the problem is but I have no idea how to fix it. Basicly I call these two lines of code inside the viewDidAppear, **[self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];** which I think is causing the crash because it uses oldcheckedIndexpath before its set up if I take these two line out of that method and put them in the init or something like that it works fine but just dosnt actually scroll.. but the table shows up fine etc/ – C.Johns Nov 02 '11 at 02:19
  • I have a new updated question that I think is the true source of the problem http://stackoverflow.com/q/7975163/807400 – C.Johns Nov 02 '11 at 02:49
  • solved.. thanks guys The problem was that the scroll to was being called before the table had time to set itself up so added a shore delay to a new method I made called that from viewdidload and now its working perfectly! what a stupid pain in the butt! – C.Johns Nov 02 '11 at 03:13

1 Answers1

0

I had a similar problem in my project where I was deleting objects from my datasource and then immediately calling [self.tableview reloadData];

What I did to solve the issue was call:

dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
                [self.tableView reloadData];
            });

This basically calls reloadData in the next run loop. You can do a similar thing using performSelector:afterDelay.

So my suggestion is try calling:

dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
  [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

Note: You don't need to actually have a delay time specified, these 2 techniques force the method to be called at the start of the next run loop.

Michael Gaylord
  • 7,282
  • 8
  • 50
  • 47