首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cellForRowAtIndexPath NSRangeException

cellForRowAtIndexPath NSRangeException
EN

Stack Overflow用户
提问于 2017-05-17 11:58:08
回答 3查看 379关注 0票数 1
代码语言:javascript
复制
Application Specific Information:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -'[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 3]'

代码语言:javascript
复制
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时,似乎调用了一个业务方法,该业务方法调用UITableViewcellForRowAtIndexPath方法,然后cellForRowAtIndexPath触发UIScrollViewscrollViewDidScroll方法。我在scrollViewDiddScroll方法中调用cellForRowAtIndexPath。它又回来了。但它不会递归,因为调用的下一个方法是NSArrayM removeObjectAtIndex,而不是触发UIScrollViewScrollViewDidScroll方法。然后发生了NSRangeException

有没有人知道这一点或有什么想法?谢谢。

EN

回答 3

Stack Overflow用户

发布于 2017-05-17 20:18:22

tableview的numberOfRowsInSection方法返回值大于计数数组。

数组的计数是3,您正在访问数组的第5个对象。

票数 1
EN

Stack Overflow用户

发布于 2017-05-17 20:51:46

您可以做的是检查以避免任何此类崩溃,只需查看此示例代码即可。这将是有帮助的,以防你不能重现它。

代码语言:javascript
复制
- (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;
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-17 12:12:35

你通过一个数组计数,但是你的计数太远了,因此不在界限内。您需要计算数组中单元的确切数量。

使用这个:_array count;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44015296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档