【问题标题】:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]”
【发布时间】:2010-11-10 19:43:34
【问题描述】:

我收到以下错误,对于 iphone 开发人员和目标 C 来说非常新。我非常愿意将我的项目发送给别人看看,我正在兜圈子,不知道该怎么做下一个!

2010-11-10 19:38:07.822 iShisha[2698:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** Call stack at first throw:
(
 0   CoreFoundation                      0x025f9b99 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x0274940e objc_exception_throw + 47
 2   CoreFoundation                      0x025ef695 -[__NSArrayM objectAtIndex:] + 261
 3   iShisha                             0x00003dc5 -[MapViewController tableView:cellForRowAtIndexPath:] + 1262
 4   UIKit                               0x0032dd6f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
 5   UIKit                               0x00323e02 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
 6   UIKit                               0x00338774 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
 7   UIKit                               0x003307ec -[UITableView layoutSubviews] + 242
 8   QuartzCore                          0x046d7481 -[CALayer layoutSublayers] + 177
 9   QuartzCore                          0x046d71b1 CALayerLayoutIfNeeded + 220
 10  QuartzCore                          0x046d02e0 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
 11  QuartzCore                          0x046d0040 _ZN2CA11Transaction6commitEv + 292
 12  QuartzCore                          0x04700ebb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
 13  CoreFoundation                      0x025daf4b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
 14  CoreFoundation                      0x0256fb27 __CFRunLoopDoObservers + 295
 15  CoreFoundation                      0x02538ce7 __CFRunLoopRun + 1575
 16  CoreFoundation                      0x02538350 CFRunLoopRunSpecific + 208
 17  CoreFoundation                      0x02538271 CFRunLoopRunInMode + 97
 18  GraphicsServices                    0x02ed800c GSEventRunModal + 217
 19  GraphicsServices                    0x02ed80d1 GSEventRun + 115
 20  UIKit                               0x002caaf2 UIApplicationMain + 1160
 21  iShisha                             0x00001de8 main + 102
 22  iShisha                             0x00001d79 start + 53
)
terminate called after throwing an instance of 'NSException'

[Session started at 2010-11-10 19:38:15 +0000.]
Pending breakpoint 1 - ""MapViewController.m":204" resolved
Pending breakpoint 2 - ""MapViewController.m":317" resolved
Pending breakpoint 3 - "objc_exception_throw" resolved
(gdb) 

【问题讨论】:

  • 请将代码贴在 [MapViewController tableView:cellForRowAtIndexPath:] 中

标签: iphone objective-c sdk


【解决方案1】:

在 CocoaTouch 中,表有一个委托和一个数据源。委托为表格视图发送和接收消息,数据源控制表格中的信息以及表格的页眉和页脚。数据源告诉表格要绘制多少行、多少个部分、要放置什么作为部分标题等等。

表格视图通过查询数据源来获取要绘制的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

方法。然后,在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法,tableView 要求一个单元格从数据源填充表格。在为 iPhone 编程时,表格主要由数组填充,一个包含许多其他变量(对象)的变量(对象)。你告诉一个数组你想要什么对象,通过询问

object = [array objectAtIndex:INTEGER]; //where INTEGER is an unsigned (zero or greater, no minus)

发生的情况是,您的数据源期望该表有 X 个对象,并且有 X-Y 可用。如果它认为有 10 个,但只有 9 个,那么当 table 请求第 10 个对象时,你会因为没有对象可以提供而崩溃。

在您的代码中查找 hte 行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

看看那个代码是什么。您可能在那里提供了错误的数据。

祝你好运

【讨论】:

  • 这太棒了!如果我在一个部分中需要比另一个部分更多的行会发生什么,例如,我需要在一个部分中有 6 行,而在另一个部分中只需要 1 行
  • simple: if (section == 0) { return 5;} else if (section == 1) {return 3;} 此代码将给第 0 部分(第一部分)5 行,和部分1(第 2 部分)3 行
  • 很好的解释。我发现我的问题是我的 TableView 正在使用Static Cells(在 IB 中设置)。我将其更改为Dynamic Prototypes,这样它就不仅限于静态数量的单元格。我希望对某人有所帮助。
【解决方案2】:

我认为不需要将这个单元格从静态单元格更改为动态单元格(在某些情况下,您可能需要将单元格设为静态)。可能导致此问题的原因是您可能只在情节提要的“属性检查器”部分中为表视图命名了 0 个部分。您需要做的就是将此数字增加到 1 或您可能需要的任意数量的部分。

我认为这就是@styfle 的问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多