由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [NSMutableArray objectAtIndex:]:索引1超出边界[0 .. 0]'

Jon*_*Jon 4 iphone sdk objective-c

我收到了以下错误,非常新的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) 
Run Code Online (Sandbox Code Playgroud)

Jus*_*tin 10

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

表视图通过查询数据源查询要绘制的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

方法.然后,在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

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

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

发生了什么,是你的数据源期望表的X个对象,并且有XY可用.如果它认为有10个,但只有9个,当表要求第10个对象时,你会因为没有任何对象而崩溃.

查看代码中的hte行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Run Code Online (Sandbox Code Playgroud)

并查看该代码是什么.你有可能在那里提供错误的数据.

祝好运

  • 很棒的解释.我发现我的问题是我的TableView使用的是`Static Cells`(在IB中设置).我将它改为`Dynamic Prototypes`,这样它不仅限于静态的细胞数量.我希望能有所帮助. (2认同)