UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:如何解决此问题?

时间:2015-03-06 11:57:58

标签: ios objective-c uitableview

我有三个视图控制器,在第三个视图中,我有一个按钮,它将被重定向到父视图(第一个视图控制器),这是一个tableview。所以当我点击按钮

时,我就崩溃了
- (IBAction)createThread:(id)sender {
       [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
 }

我在Stack溢出中找到了解决方案,但这没有帮助。[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]

请告诉我一个解决方案。

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 个答案:

答案 0 :(得分:0)

In viewWillAppear of your Firt Viewcontroller

添加 [tableview reloadData];

答案 1 :(得分:0)

我认为以下代码中的else部分存在问题

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];
}

假设numberOfObjects == 0将进入其他情况。你告诉tableView滚动到 indexPathForRow:0 inSection:0 。 Zero完全是一个有效的行,它是tableView的第一行。实际上,此时您的numberOfObjects为零,这意味着tableView中没有行。这会导致崩溃。因此,请删除不需要的else案例。