views:

240

answers:

2

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?

+1  A: 

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.

Tom Harrington
Thanks for the feedback. I've edited my question to include the error itself (as the site won't let me put it here - too long).
Nik1777
OK, that's not quite as useful as I had hoped, but sometimes that's how it happens. But the stack trace suggests that you can find the error fairly easily. Run the app under the debugger (Run menu --> Debug) and run it until it crashes. The debugger window should show a stack trace that contains one of your methods. Click on that and you can see the exact line where the crash occurred.
Tom Harrington
Thanks. I am not too familiar with stack traces. Is this what you were after:0x3293f984 <+0000> mov r12, #37 ; 0x250x3293f988 <+0004> svc 0x000000800x3293f98c <+0008> bcc 0x3293f9a4 <__kill+32>0x3293f990 <+0012> ldr r12, [pc, #4] ; 0x3293f99c <__kill+24>0x3293f994 <+0016> ldr r12, [pc, r12]0x3293f998 <+0020> b 0x3293f9a0 <__kill+28>0x3293f99c <+0024> andeq r9, r7, r0, lsl r70x3293f9a0 <+0028> bx r120x3293f9a4 <+0032> bx lrThere was a red arrow by the 3rd line
Nik1777
It seems comments don't support carriage returns so when I said the 3rd line I meant this bit: 0x3293f98c <+0008> bcc 0x3293f9a4 <__kill+32>
Nik1777
I saw that in the question. It's useless to me but it looks to me as though using the debugger would be very useful in this case, which is why I suggested it and still recommend it.
Tom Harrington
A: 

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.

Nik1777
You're just dodging a bug here. It'll almost certainly come back to bite at some point. Learn to use the debugger or suffer the crashes.
Tom Harrington