Application Specific Information:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -'[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 3]'
0 CoreFoundation __exceptionPreprocess + 124
1 libobjc.A.dylib objc_exception_throw + 56
2 CoreFoundation -[__NSArrayM removeObjectAtIndex:] + 0
3 UIKit -[UITableView cellForRowAtIndexPath:] + 252
4 someApp -[businessViewClass methodA:] (businessViewClass.m:298)
5 someApp -[businessViewClass scrollViewDidScroll:] (businessViewClass.m:292)
6 UIKit -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 76
7 UIKit -[UIScrollView setContentOffset:] + 460
8 UIKit -[UITableView setContentOffset:] + 300
9 UIKit -[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary] + 60
10 UIKit -[UIScrollView setContentSize:] + 128
11 UIKit -[UITableView _applyContentSizeDeltaForEstimatedHeightAdjustments:] + 56
12 UIKit -[UITableViewRowData setHeight:forRowAtIndexPath:] + 572
13 UIKit __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 3016
14 UIKit +[UIView(Animation) performWithoutAnimation:] + 80
15 UIKit -[UITableView _configureCellForDisplay:forIndexPath:] + 460
16 UIKit -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 788
17 UIKit -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 80
18 UIKit -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
19 UIKit -[UITableView cellForRowAtIndexPath:] + 136
20 someApp -[businessViewClass methodA:] (businessViewClass.m:298)
21 UIKit -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 76
22 UIKit -[UIScrollView setContentOffset:] + 460
23 UIKit -[UITableView setContentOffset:] + 300
24 UIKit -[UIScrollView _smoothScrollWithUpdateTime:] + 2400
25 QuartzCore CA::Display::DisplayLinkItem::dispatch() + 40
26 QuartzCore CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 424
27 IOKit 0x000000018334de54 0x183348000 + 24148
28 CoreFoundation __CFMachPortPerform + 180
29 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
30 CoreFoundation __CFRunLoopDoSource1 + 436
31 CoreFoundation __CFRunLoopRun + 1800
32 CoreFoundation CFRunLoopRunSpecific + 384
33 GraphicsServices GSEventRunModal + 180
34 UIKit UIApplicationMain + 204
35 someApp main (main.m:15)
36 libdyld.dylib 0x0000000182b528b8 0x182b50000 + 10424事情就是这样发生的。
我在app online中发现了一个bug,但我不能用项目代码重现它。当滚动UIScrollview时,似乎调用了一个业务方法,该业务方法调用UITableView的cellForRowAtIndexPath方法,然后cellForRowAtIndexPath触发UIScrollView的scrollViewDidScroll方法。我在scrollViewDiddScroll方法中调用cellForRowAtIndexPath。它又回来了。但它不会递归,因为调用的下一个方法是NSArrayM removeObjectAtIndex,而不是触发UIScrollView的ScrollViewDidScroll方法。然后发生了NSRangeException。
有没有人知道这一点或有什么想法?谢谢。
发布于 2017-05-17 20:18:22
tableview的numberOfRowsInSection方法返回值大于计数数组。
数组的计数是3,您正在访问数组的第5个对象。
发布于 2017-05-17 20:51:46
您可以做的是检查以避免任何此类崩溃,只需查看此示例代码即可。这将是有帮助的,以防你不能重现它。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.yourArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (self.yourArray.count > indexPath.row)
{
// your code for the cell
}
return cell;
}发布于 2017-05-17 12:12:35
你通过一个数组计数,但是你的计数太远了,因此不在界限内。您需要计算数组中单元的确切数量。
使用这个:_array count;
https://stackoverflow.com/questions/44015296
复制相似问题