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?
Perhaps a bit of a fudge but it works if i clear the table first:
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.