简体   繁体   English

iOS:致命异常:NSRangeException ***-[__ NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]

[英]iOS: Fatal Exception: NSRangeException *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

this is the crash I am receiving right now. 这是我现在正在接收的崩溃。 Can someone please help me figure out how to resolve this? 有人可以帮我找出解决方法吗? It occurs on this line UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView indexPathForRowAtPoint:point]]; 它发生在这行UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView indexPathForRowAtPoint:point]]; I thought if I check if indexPath was valid it would resolve it, but it didn't. 我以为如果我检查indexPath是否有效,它将解决该问题,但是没有解决。 Any way to stop this from occurring? 有什么办法可以阻止这种情况的发生? Any tips or suggestions are appreciated. 任何提示或建议,不胜感激。 I will post what I tried to do below. 我将在下面发布我尝试做的事情。

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
if (indexPath) {
UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView    indexPathForRowAtPoint:point]];
.... rest of my code
  }

You might reload the table view before initialising array , or may be your array is empty when you use it ! 您可能在初始化数组之前重新加载表视图,或者使用时可能是数组为空!

Like when you use : 就像您使用时:

NSString *xyz = [self.your_array objectAtIndex:indexPath.row]; 

in cellForRowAtIndexPath , Try fllowing code that wont crash your app ! 在cellForRowAtIndexPath中,尝试使用不会使您的应用程序崩溃的代码!

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

static NSString *CellIdentifier = @"xyz_cell";
long nodeCount = [self.your_array count];

if (nodeCount == 0 && indexPath.row == 0)
{
    Home_CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[Home_CellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] ;
        cell.lbl.textAlignment = NSTextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.lbl.text = @"No Posts yet !";

    return cell;
}


else{

Home_CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[Home_CellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} // Your Code Goes HERE .... NSString *xyz = [self.your_array objectAtIndex:indexPath.row]; }

so , for empty array it wont find value for key one and your app wont crash ! 因此,对于空数组,它将找不到键一的值,并且您的应用程序将不会崩溃!

I hope it helps , 希望对您有所帮助,

Your tableView doesn't have more than a single row I suspect (or none?). 我怀疑tableView的行数不多(或没有?)。

As your error message indicates, you're trying to reference object at index 1, but the bounds of the array are 0 .. 0. 如错误消息所示,您正在尝试引用索引1处的对象,但数组的边界为0 .. 0。

If you're executing this before the tableView is initialized you'll need to change where you execute it, or perhaps you forgot to implement the tableView:numberOfRowsInSection: delegate method? 如果要在初始化tableView之前执行此操作,则需要更改执行位置,或者您可能忘记实现tableView:numberOfRowsInSection:委托方法?

As you told in your question that error coming in this line, it means that indexPath is holding some reference Try change this line 正如您在问题中告诉您的是,此行中出现错误,这意味着indexPath持有一些参考尝试更改此行

if (indexPath) {
    UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView  indexPathForRowAtPoint:point]];
}

to

if (indexPath) {
        UITableViewCell* cell=[_tableView cellForRowAtIndexPath:indexPath]; 
    }

But, i suspect that you are not having more than 1 item in your tableview. 但是,我怀疑您的表视图中没有超过1个项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'NSRangeException',原因:'* -[__NSArrayM objectAtIndex:]:索引 2 超出范围 [0 .. 1]' - 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' 'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]' - 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' UITableView 边界终止应用程序 'NSRangeException' -[__NSArrayM objectAtIndex:]: 索引 2 超出边界 [0 .. 1]' - UITableView bounds Terminating app 'NSRangeException' -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 5 超出空数组的边界' - NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array' “ ***由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***-[__ NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]'” - “*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'” 由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:索引2超出边界[0 .. 1]' - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' 由于未捕获的异常'NSRangeException'终止应用程序,原因: - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界' - Terminating app due to uncaught exception 'NSRangeException', reason: -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' ***由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***-[__ NSArrayM objectAtIndex:]:索引5超出范围[0 .. 4]' - *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]' 如何修复由于未捕获的异常'NSRangeException'导致的tableview崩溃,原因:'*** - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]' - how to fix tableview crash due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 由于未捕获的异常'NSRangeException'终止应用程序,原因: - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界' - 2 - Terminating app due to uncaught exception 'NSRangeException', reason: -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' - 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM