Tutorial :Scroll table to top causes crash



Question:

I've tried:

- (IBAction)openSearch {  [tblSimpleTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];  [searchBar becomeFirstResponder];  }  

and

- (IBAction)openSearch {  [self.tblSimpleTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];  [searchBar becomeFirstResponder];  }  

to get the table to scroll to the top. Both work when the table is only slightly scrolled away from the top but otherwise crashes with a "beyond bounds" error.

Any ideas. I am fairly new to this. Thanks.

--Edit--

Thanks for the feedback. Here's the precise error

2010-02-15 00:49:02.010 MyApp [2935:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'  2010-02-15 00:49:02.016 MyApp [2935:207] Stack: (  861696817,  860329709,  861252493,  861252395,  845801683,  845954223,  30161,  835250561,  835249847,  834989551,  834983899,  834971003,  805539851,  805539363,  805538115,  805537449,  805560369,  861158231,  861448761,  861447005,  861059891,  861060063,  834770799,  834765939,  10065,  9980  )  terminate called after throwing an instance of 'NSException'  Program received signal:  “SIGABRT”.  

I don't get this problem when I scroll manually. The table is populated from an NSMutableArray. Does this help at all?


Solution:1

Neither of those will cause a crash on their own, so your crash is a side-effect of the scrolling and not a direct result of the code you pasted. It would help to know the exact error you're seeing, but a likely cause is that you have an NSArray and you're trying to get an element at an index too large for it (or possibly negative). It's likely that one of your table view's delegate or data source methods are the direct cause of the crash, and that scrolling is only relevant because it causes a bug there to manifest itself.


Solution:2

Perhaps a bit of a fudge but it works if i clear the table first:

searching=YES;  [tblSimpleTable reloadData];  [tblSimpleTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];  

for searching=yes the table is set to reload and empty array. I guess I could repopulate the table after doing this but I don't really need to in this case. I think the problem had something to do with the table cells being redrawn when they came into view to save memory.

Anyhow - for people with the same problem: reload the table with an empty array, scroll to the top, reload the table again with the old array. Hope this helps. Maybe someone will come up with a better way to do this.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »