UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition: How to solve This issue?

I have three view controllers, in the third view I have a button which will be redirect to parent view(First view controller), Which is a tableview. So I'm getting the this crash when i click on the button,

- (IBAction)createThread:(id)sender {
       [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
 }

I have found solutions in Stack overflow but that is not helped.[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]

Please tell me a solution.

CRASH LOGS:

      Terminating app due to uncaught exception 'NSRangeException', reason: '-    [UITableView  

  contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond   bounds (0) for section (0).'
  *** First throw call stack:
 (
0   CoreFoundation                      0x000000010efeff35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010ec88bb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010efefe6d +[NSException raise:format:] + 205
3   UIKit                               0x000000010d616d7c -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 1178
4   UIKit                               0x000000010d61794a -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 33
5   Inxed                               0x000000010b798fd4 -[IXDetailedMessageViewController openCommentView:] + 7396
6   Inxed                               0x000000010b794835 -[IXDetailedMessageViewController viewDidAppear:] + 149
7   UIKit                               0x000000010d65a871 -[UIViewController _setViewAppearState:isAnimating:] + 567
8   UIKit                               0x000000010d65b03b -[UIViewController _endAppearanceTransition:] + 304
9   UIKit                               0x000000010d632dd9 -[UIPresentationController transitionDidFinish:] + 699
10  UIKit                               0x000000010dbf8298 -[_UIFullscreenPresentationController transitionDidFinish:] + 71
11  UIKit                               0x000000010d635301 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 133
12  UIKit                               0x000000010dc87ac3 -[_UIViewControllerTransitionContext completeTransition:] + 110
13  UIKit                               0x000000010dca1d89 -[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 2694
14  UIKit                               0x000000010d6351d3 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 1842
15  UIKit                               0x000000010d661ab0 +[UIViewController _scheduleTransition:] + 103
16  UIKit                               0x000000010d634a92 -[UIPresentationController runTransitionForCurrentState] + 1243
17  UIKit                               0x000000010d5a25ce +[UIView(Animation) performWithoutAnimation:] + 65
18  UIKit                               0x000000010d6663c4 -[UIViewController _dismissViewControllerWithAnimationController:interactionController:completion:] + 685
19  UIKit                               0x000000010d5a25ce +[UIView(Animation) performWithoutAnimation:] + 65
20  UIKit                               0x000000010d666036 -[UIViewController _dismissViewControllerWithTransition:from:completion:] + 880
21  UIKit                               0x000000010d661a18 +[UIViewController _performWithoutDeferringTransitions:] + 101
22  UIKit                               0x000000010d66573f -[UIViewController dismissViewControllerWithTransition:completion:] + 627
23  UIKit                               0x000000010d667814 -[UIViewController dismissViewControllerAnimated:completion:] + 255
24  Inxed                               0x000000010b924ad5 -[IXConfirmFeedbackPage createThread:] + 1093
25  UIKit                               0x000000010d5328be -[UIApplication sendAction:to:from:forEvent:] + 75
26  UIKit                               0x000000010d639410 -[UIControl _sendActionsForEvents:withEvent:] + 467
27  UIKit                               0x000000010d6387df -[UIControl touchesEnded:withEvent:] + 522
28  UIKit                               0x000000010d578308 -[UIWindow _sendTouchesForEvent:] + 735
29  UIKit                               0x000000010d578c33 -[UIWindow sendEvent:] + 683
30  UIKit                               0x000000010d5459b1 -[UIApplication sendEvent:] + 246
31  UIKit                               0x000000010d552a7d _UIApplicationHandleEventFromQueueEvent + 17370
32  UIKit                               0x000000010d52e103 _UIApplicationHandleEventQueue + 1961
33  CoreFoundation                      0x000000010ef25551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
34  CoreFoundation                      0x000000010ef1b41d __CFRunLoopDoSources0 + 269
35  CoreFoundation                      0x000000010ef1aa54 __CFRunLoopRun + 868
36  CoreFoundation                      0x000000010ef1a486 CFRunLoopRunSpecific + 470
37  GraphicsServices                    0x00000001103899f0 GSEventRunModal + 161
38  UIKit                               0x000000010d531420 UIApplicationMain + 1282
39  Inxed                               0x000000010b8d2703 main + 115
40  libdyld.dylib                       0x000000010fa86145 start + 1
41  ???                                 0x0000000000000001 0x0 + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException

Opencommentview:

 - (IBAction)openCommentView:(id)sender
{

 NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
ThreadInfo *info=[self selectedThreadInfo];
if(info!=nil)
{
    if([info.isThreadBlocked boolValue] || [info.isThreadRemoved boolValue] ||  [info.isThreadEnded boolValue])
    {
        self.tapButton.alpha=0.5;

        UIAlertView *errorAlert=[[UIAlertView alloc]initWithTitle:@"This thread has been closed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
    else
    {

        self.view.backgroundColor=[UIColor whiteColor];
        self.commentTextView.hidden=NO;
        self.commentTextView.text=@"";
        self.commentTextCountLabel.text=@"";
        float commentViewY=0.0,tableViewHeight=0.0,countLabelY=0.0;
        if(IS_IPHONE5)
        {
            if(ver_float>=8.0)
            {
                commentViewY=200.0;
                tableViewHeight=180.0;
                countLabelY=90;
                self.commentTextView.frame=CGRectMake(self.commentTextView.frame.origin.x, self.commentTextView.frame.origin.y, self.commentTextView.frame.size.width, 112);
            }
            else
            {
                commentViewY=240.0;
                tableViewHeight=220.0;
                countLabelY=90;
                self.commentTextView.frame=CGRectMake(self.commentTextView.frame.origin.x, self.commentTextView.frame.origin.y, self.commentTextView.frame.size.width, 108);
            }




        }
        else
        {
            if(ver_float>=8.0)
            {
                countLabelY=56;

                commentViewY=150;
                tableViewHeight=130;
                self.commentTextView.frame=CGRectMake(self.commentTextView.frame.origin.x, self.commentTextView.frame.origin.y, self.commentTextView.frame.size.width, 75);

            }
            else
            {
                countLabelY=76;
                commentViewY=170;
                tableViewHeight=150;
                self.commentTextView.frame=CGRectMake(self.commentTextView.frame.origin.x, self.commentTextView.frame.origin.y, self.commentTextView.frame.size.width, 90);
            }
        }
        id  sectionInfo =
        [[_fetchedResultsController sections] objectAtIndex:0];

        NSInteger count = [sectionInfo numberOfObjects];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count-1 inSection:1];

        [self.commentTextView becomeFirstResponder];
        self.commentTextCountLabel.frame=CGRectMake(self.commentTextCountLabel.frame.origin.x, countLabelY, self.commentTextCountLabel.frame.size.width, self.commentTextCountLabel.frame.size.height);

        self.isCommentsOpen=YES;
        self.tapButton.hidden=YES;
        self.postButton.hidden=NO;
        self.postButton.enabled=NO;
        if(self.isFromComments)
        {
            self.commentView.frame=CGRectMake(self.commentView.frame.origin.x, commentViewY, self.commentView.frame.size.width, self.commentView.frame.size.height);
            self._mDetailedMessageTableView.frame=CGRectMake(self._mDetailedMessageTableView.frame.origin.x,0, self._mDetailedMessageTableView.frame.size.width, tableViewHeight);
            // self.commentTextView.clipsToBounds = NO;
            id  sectionInfo =
            [[_fetchedResultsController sections] objectAtIndex:0];

            if([sectionInfo numberOfObjects]!=0)
            {
                [self._mDetailedMessageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

            }
            else
            {
                [self._mDetailedMessageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
            }

        }
        else
        {
            [UIView animateWithDuration:0.2
                             animations:^{

                                 self.commentView.frame=CGRectMake(self.commentView.frame.origin.x, commentViewY, self.commentView.frame.size.width, self.commentView.frame.size.height);
                                 self._mDetailedMessageTableView.frame=CGRectMake(self._mDetailedMessageTableView.frame.origin.x,0, self._mDetailedMessageTableView.frame.size.width, tableViewHeight);


                                 self.commentTextView.clipsToBounds = NO;
                                 id  sectionInfo =
                                 [[_fetchedResultsController sections] objectAtIndex:0];

                                 if([sectionInfo numberOfObjects]!=0)
                                 {
                                     [self._mDetailedMessageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

                                 }
                                 else
                                 {
                                     [self._mDetailedMessageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
                                 }

                             }
                             completion:^(BOOL finished){

                             }];
        }
    }
}
}

2 answers

  • answered 2015-03-06 12:34 Robin

    In viewWillAppear of your Firt Viewcontroller
    

    add [tableview reloadData];

  • answered 2015-03-06 14:02 Anil Varghese

    I think the else part in the below code is making problem

    if([sectionInfo numberOfObjects]!=0)
    {
       [self._mDetailedMessageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    
    }
    else
    {
      [self._mDetailedMessageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
    

    Assume numberOfObjects == 0 will enter into else case. There you are telling tableView to scroll to indexPathForRow:0 inSection:0. Zero is perfectly a valid row, which is the first row of the tableView. In fact at this moment your numberOfObjects is zero which means there are no rows in the tableView. This would cause a crash. So remove the else case which is not required.