[英]UITableview reloadRowsAtIndexPaths and crash
我有這樣的代碼。 當我使用異常斷點進行調試時,在reloadRow
時reloadRow
。
- (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
)
我相信錯誤所指的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]'
這表明,如果在cellForRowAtIndexPath
完成之前調用setData
的完成處理程序,則UITableView
將嘗試再調用一個單元格,然后它具有-請注意,我的錯誤表明索引8超出了范圍[0 .. 7],而UITableView
具有有8個可見的單元格
為了解決該問題,我建議不要調用reloadRowAtIndexPath
,而是更新單元格直接包含的任何顯示的值。
您的代碼顯示錯誤,因為在其中設置數據的塊可能會產生一點點延遲。 我的意思是,您要從其委托方法重新加載表視圖。 並且從某個塊調用重裝,這可能是崩潰的原因。 您可以做一件事,而不是重新加載特定的行,然后嘗試重新加載整個表。 希望它能工作。
把事情簡單化。 每個節中的行數應等於Array中的對象。 假設您在第1節中傳遞了4個計數(從index == 0開始)應該等於Array對象。
由於ROW計數而崩潰,並且數組中的對象不匹配。 如果不匹配,您將得到
“原因:' *-[__ NSArrayM objectAtIndex:]:索引4超出范圍[0 .. 3]'”
界限[0..3]表示Array只有4個對象,它正嘗試在索引4處獲得第5個對象。
請檢查它,並且在Array對象中應該有正確的計數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.