崩溃时调用selectRowAtIndexPath:动画:的scrollPosition:(Cras

2019-10-19 00:28发布

我有以下方法,用于选择当前中心视图控制器行:

- (void)_updateSelection
{
    [self.tableView reloadData];

    [_viewControllers enumerateKeysAndObjectsUsingBlock:^(NSString *path, UIViewController *viewController, BOOL *stop) {
        if (viewController == self.revealSideViewController.rootViewController) {
            NSInteger row = [self indexOfPath:path];
            if (row != NSNotFound && row < [self.tableView numberOfRowsInSection:0]) {
                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            }

            *stop = YES;
        }
    }];
}

正如你所看到的,我有几个检查,以确保一切都有效。 根据表视图,该行是该部分的边界内。 而大部分的时间,这工作得很好。 因为表视图不断失去其选择时reloadData被调用时,我把这个当我需要做出改变。 我也把它在viewWillAppear: 。 这里是它偶尔会崩溃。

崩溃的输出是:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x041415e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x03ec48b6 objc_exception_throw + 44
    2   CoreFoundation                      0x040e24e6 -[__NSArrayM objectAtIndex:] + 246
    3   UIKit                               0x022aaa5c -[UITableView cellForRowAtIndexPath:] + 235
    4   UIKit                               0x022ad6f0 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1320
    5   UIKit                               0x022ad983 -[UITableView selectRowAtIndexPath:animated:scrollPosition:] + 63
    6   The City                            0x000073a2 __40-[TCMainMenuController _updateSelection]_block_invoke + 578
    7   CoreFoundation                      0x041cb13a __65-[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 106
    8   CoreFoundation                      0x041cb03e -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 190
    9   CoreFoundation                      0x0412f525 -[NSDictionary enumerateKeysAndObjectsUsingBlock:] + 53
    10  The City                            0x0000713c -[TCMainMenuController _updateSelection] + 220
    11  The City                            0x00006999 -[TCMainMenuController viewWillAppear:] + 105
    12  UIKit                               0x022e2bfa -[UIViewController _setViewAppearState:isAnimating:] + 419
    13  UIKit                               0x022e3108 -[UIViewController __viewWillAppear:] + 114
    14  UIKit                               0x022e40c7 -[UIViewController viewWillMoveToWindow:] + 387
    15  UIKit                               0x02227384 -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 619
    16  UIKit                               0x02232f60 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 470
    17  UIKit                               0x022269ed -[UIView(Hierarchy) insertSubview:belowSubview:] + 55
    18  The City                            0x0034c6b4 -[PPRevealSideViewController pushViewController:onDirection:withOffset:animated:forceToPopPush:completion:] + 2260
    19  The City                            0x000afc9c -[TCRootViewController pushViewController:onDirection:withOffset:animated:forceToPopPush:completion:] + 428
    20  The City                            0x0034bc9c -[PPRevealSideViewController pushViewController:onDirection:withOffset:animated:completion:] + 236
    21  The City                            0x0034b8a4 -[PPRevealSideViewController pushViewController:onDirection:animated:completion:] + 212
    22  The City                            0x0034b7b5 -[PPRevealSideViewController pushViewController:onDirection:animated:] + 149
    23  The City                            0x000af832 -[TCRootViewController showMenu] + 162
    24  The City                            0x00009a13 +[App showMenu] + 83
    25  The City                            0x00008611 -[TCMainMenuController showMenu:] + 81
    26  libobjc.A.dylib                     0x03ed6874 -[NSObject performSelector:withObject:withObject:] + 77
    27  UIKit                               0x021cd0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    28  UIKit                               0x024a1c9b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    29  libobjc.A.dylib                     0x03ed6874 -[NSObject performSelector:withObject:withObject:] + 77
    30  UIKit                               0x021cd0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    31  UIKit                               0x021cd04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    32  UIKit                               0x022c50c1 -[UIControl sendAction:to:forEvent:] + 66
    33  UIKit                               0x022c5484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    34  UIKit                               0x022c4733 -[UIControl touchesEnded:withEvent:] + 641
    35  UIKit                               0x0220a51d -[UIWindow _sendTouchesForEvent:] + 852
    36  UIKit                               0x0220b184 -[UIWindow sendEvent:] + 1232
    37  UIKit                               0x021dee86 -[UIApplication sendEvent:] + 242
    38  UIKit                               0x021c918f _UIApplicationHandleEventQueue + 11421
    39  CoreFoundation                      0x040ca83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    40  CoreFoundation                      0x040ca1cb __CFRunLoopDoSources0 + 235
    41  CoreFoundation                      0x040e729e __CFRunLoopRun + 910
    42  CoreFoundation                      0x040e6ac3 CFRunLoopRunSpecific + 467
    43  CoreFoundation                      0x040e68db CFRunLoopRunInMode + 123
    44  GraphicsServices                    0x04f8b9e2 GSEventRunModal + 192
    45  GraphicsServices                    0x04f8b809 GSEventRun + 104
    46  UIKit                               0x021cbd3b UIApplicationMain + 1225
    47  The City                            0x000026ad main + 141
    48  libdyld.dylib                       0x0468270d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

使用调试器,我可以看到-visibleCells ,而不是空的,没有我想选择单元格(但它应该,除了它是屏幕外)。

Answer 1:

您的问题,从这一事实源于-reloadData当你将控制权返回运行循环运行。 你现在正在做它的方式会导致崩溃,因为-reloadData不被直到离开该块之后进行。

解决这个问题的方法之一是派遣一个块中的下一个运行循环运行:

[self.tableView reloadData];
dispatch_async(dispatch_get_main_thread(), ^{
    [_viewControllers enumerateKeysAndObjectsUsingBlock:^(NSString *path, UIViewController *viewController, BOOL *stop) {
        if (viewController == self.revealSideViewController.rootViewController) {
            NSInteger row = [self indexOfPath:path];
            if (row != NSNotFound && row < [self.tableView numberOfRowsInSection:0]) {
                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            }

            *stop = YES;
        }
    }];
});

看到这个讨论: 如何判断时的UITableView已完成ReloadData?



文章来源: Crash when calling selectRowAtIndexPath:animated:scrollPosition: