Skip to main content

error with scrollToRowAtIndexPath



I am getting an error when using my application that used tableviews, the error looks like this.







2012-01-19 10:19:51.442 bcode[1176:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (3) beyond bounds (3) for section (0).'

*** First throw call stack:

(0x16c3052 0x1920d0a 0x166ba78 0x166b9e9 0x68a136 0x3bfdf 0x6c2fbf 0x6c32d4 0x6c35d7 0x6d2666 0x87890e 0x878c17 0x878c86 0x62c499 0x62c584 0x2718e00 0x13614f0 0x15fa833 0x15f9db4 0x15f9ccb 0x1d05879 0x1d0593e 0x5fba9b 0x2838 0x2795 0x1)

terminate called throwing an exception(gdb)







What is happening is that when the user drills down my navigation view they can select a cell from the tableview of each view, which later will be used to build a search string.





However I allow them to go back and change their selection if they made an error.. this error happens when the user changes the value of the parent view then going into the subview only if the previous selection was outside the number of entries in the current tableview..





normally this wouldn't be a big problem however, inside viewDidAppear I am calling a method that scrolls to the previously selected cell... which obviously is what is breaking the app and giving me the error.







- (void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

//Scroll to previously selected value

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

}







how can I stop this from executing I have tried about a 100 different if statments in the parent view and the subview catching the new indexpath and checking agains oldselected indexpaths then setting







oldCheckedIndexPath = nil;







however somehow it always manages to mess up anyway.


Comments

  1. The clean way, assuming self is the tableView datasource:

    Check the number of sections in the tableview:

    if ( [self numberOfSectionsInTableView:self.tableView]
    > oldCheckedIndexPath.section


    and check the number of rows in that section:

    && [self tableView:self.tableView numberOfRowsInSection:
    oldCheckedIndexPath.section] > oldCheckedIndexPath.row )
    {
    [self.tableView scrollToRowAtIndexPath: // etc etc etc


    Or, the quick hack:

    @try {
    [self.tableView scrollToRowAtIndexPath: // etc etc etc
    }
    @catch ( NSException e )
    {
    NSLog(@"bummer: %@",e);
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.