2017-01-10 175 views
0

我有这样的代码。当我用异常断点进行调试时,它会在我reloadRow时崩溃。UITableview reloadRowsAtIndexPaths和崩溃

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 0) 
     return ceil(self.folders.count/ 2); 

    else if (section <= self.documents.count) { 
     DocumentsCollection *dc = self.documents[section - 1]; 
     return dc.attachments.count; 
    } 

    return 0; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

//.......... 
[cell setData:dc.attachments[indexPath.row] andReload:^{ 

     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

}]; 
} 

我在我的单元格中使用约束,我所有的数据都是真的正确。我正在尝试阅读错误信息并在这里找到,但我不明白。请好好解释我应该怎么做。我完全不明白为什么它说

“的原因: '*** - [__ NSArrayM objectAtIndex:]:指数4超出范围[0..3]'”

*** First throw call stack: 
(
    0 CoreFoundation      0x000000011334fd4b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000011252d21e objc_exception_throw + 48 
    2 CoreFoundation      0x00000001132812eb -[__NSArrayM objectAtIndex:] + 203 
    3 UIKit        0x0000000110c61df6 -[UITableView _existingCellForRowAtIndexPath:] + 159 
    4 UIKit        0x0000000110c8555c -[UITableView _heightForRowAtIndexPath:] + 109 
    5 UIKit        0x0000000110ebd783 -[UISectionRowData heightForRow:inSection:canGuess:] + 263 
    6 UIKit        0x0000000110ec5601 -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:] + 1016 
    7 UIKit        0x0000000110ec5707 -[UITableViewRowData rectForGlobalRow:heightCanBeGuessed:] + 142 
    8 UIKit        0x0000000110ec64b9 -[UITableViewRowData globalRowsInRect:canGuess:] + 1101 
    9 UIKit        0x0000000110fa5cf3 -[_UITableViewUpdateSupport(Private) _visibleRowRangePlusAdjoining] + 96 
    10 UIKit        0x0000000110fa5dc8 -[_UITableViewUpdateSupport(Private) _faultInRealHeightsOfNeededCells] + 138 
    11 UIKit        0x0000000110fb81c7 -[_UITableViewUpdateSupport _setupAnimations] + 164 
    12 UIKit        0x0000000110c544dd -[UITableView _updateWithItems:updateSupport:] + 3054 
    13 UIKit        0x0000000110c4c90f -[UITableView _endCellAnimationsWithContext:] + 17960 
    14 UIKit        0x0000000110c63f62 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 331 
    15 UIKit        0x0000000126e4d4a2 -[UITableViewAccessibility reloadRowsAtIndexPaths:withRowAnimation:] + 73 
    16 iFeedback       0x000000010dc809ad __56-[DocumentVCWithFolder tableView:cellForRowAtIndexPath:]_block_invoke + 125 
    17 iFeedback       0x000000010dbeb6e8 -[DocumentDetailCell observeValueForKeyPath:ofObject:change:context:] + 552 
    18 Foundation       0x000000010f25bdec NSKeyValueNotifyObserver + 351 
    19 Foundation       0x000000010f25b687 NSKeyValueDidChange + 495 
    20 Foundation       0x000000010f33cb95 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:usingBlock:] + 986 
    21 Foundation       0x000000010f220b01 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 60 
    22 Foundation       0x000000010f2e80df _NSSetSizeValueAndNotify + 285 
    23 UIKit        0x000000011144fa84 -[UICollectionView _updateVisibleCellsNow:] + 745 
    24 UIKit        0x0000000111456725 -[UICollectionView layoutSubviews] + 313 
    25 UIKit        0x0000000110bd2ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 
    26 QuartzCore       0x00000001106e6bf8 -[CALayer layoutSublayers] + 146 
    27 QuartzCore       0x00000001106da440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    28 UIKit        0x0000000110bc0928 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1509 
    29 iFeedback       0x000000010dbeb4a9 -[DocumentDetailCell setUpTag] + 553 
    30 iFeedback       0x000000010dbeb24a -[DocumentDetailCell setData:andReload:] + 154 
    31 iFeedback       0x000000010dc8078b -[DocumentVCWithFolder tableView:cellForRowAtIndexPath:] + 1995 
    32 UIKit        0x0000000110c7b584 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 757 
    33 UIKit        0x0000000110c7b7e2 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 
    34 UIKit        0x0000000110c4f2b0 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295 
    35 UIKit        0x0000000110c84b64 -[UITableView _performWithCachedTraitCollection:] + 110 
    36 UIKit        0x0000000110c6b3be -[UITableView layoutSubviews] + 222 
    37 UIKit        0x0000000110bd2ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 
    38 QuartzCore       0x00000001106e6bf8 -[CALayer layoutSublayers] + 146 
    39 QuartzCore       0x00000001106da440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    40 QuartzCore       0x00000001106da2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    41 QuartzCore       0x0000000110668318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 
    42 QuartzCore       0x00000001106953ff _ZN2CA11Transaction6commitEv + 475 
    43 QuartzCore       0x0000000110695d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113 
    44 CoreFoundation      0x00000001132f4267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    45 CoreFoundation      0x00000001132f41d7 __CFRunLoopDoObservers + 391 
    46 CoreFoundation      0x00000001132d8f8e __CFRunLoopRun + 1198 
    47 CoreFoundation      0x00000001132d8884 CFRunLoopRunSpecific + 420 
    48 GraphicsServices     0x0000000115340a6f GSEventRunModal + 161 
    49 UIKit        0x0000000110b0dc68 UIApplicationMain + 159 
    50 iFeedback       0x000000010dc60ebf main + 111 
    51 libdyld.dylib      0x00000001138da68d start + 1 
) 
+1

我认为这意味着你正在尝试重新加载第4行,但你的'UITableView'中只有3行。 –

+0

这是可能的,但我从“cellForRowAtIndexPath”直接得到indexPath。为什么我有3排,我想重新加载第4排? –

+0

'numberOfRowsInSection'怎么样? – nynohu

回答

0

我认为Array这个错误所指的实际上是UITableView维护的可重用单元队列。当您拨打reloadRowsAtIndexPath时,tableView将询问其委托人处于相同indexPath的另一个单元格。

通过添加日志成cellForRowAtIndexPath我的日志看像第一和最后一行:

Start for cell at [0, 9] 
Start for cell at [0, 9] 
End for cell at [0, 9] 
End for cell at [0, 9] 
Start for cell at [0, 10] 
Start for cell at [0, 10] 
End for cell at [0, 10] 
End for cell at [0, 10] 
Start for cell at [1, 0] 
Start for cell at [1, 0] 
End for cell at [1, 0] 
End for cell at [1, 0] 
Start for cell at [1, 1] 
2017-01-10 16:48:05.530 TableView[25012:201598] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]' 

这表明,如果你setData的完成处理程序获取cellForRowAtIndexPath完成之前叫,你UITableView将尝试调用一个更多的单元格然后它有 - 请注意,我的错误说索引8越界[0..7],我的UITableView有8个可见单元格。

为了解决这个问题,我建议不要致电reloadRowAtIndexPath,而是直接更新你的单元格包含的任何显示值。

0

您的代码显示错误,因为您在其中设置数据的块可能会产生一点点延迟。 我的意思是,您正在从其委托方法重新加载表视图。而重新加载是从一个可能是崩溃原因的块中调用的。 你可以做一件事,而不是重新加载特定的行,尝试重新加载整个表。 希望它能起作用。

+0

我在处理块和表视图时遇到了这个问题。 –

0

保持简单。 每个部分的行数应该等于Array中的对象。 假设您在第1部分(从索引== 0开始)中传递4个计数应该等于您的Array对象。

由于行数和阵列中的对象不匹配而导致崩溃。 如果不是比你匹配会得到

“的理由: '* - [__ NSArrayM objectAtIndex:]:指数4超出范围[0..3]'”

界[0..3]意味着数组只有4个对象,并且它试图获取索引4处的第5个对象。
请检查它并且在Array对象中应该有适当的计数。