【问题标题】:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4] '由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]”
【发布时间】:2016-02-18 17:30:01
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    SimpleeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.TitleLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.PlaceLabel.text = [placeData objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.RankingsLabel.text = [rankingsData objectAtIndex:indexPath.row];
    cell.StatusLabel.text = [statusData objectAtIndex:indexPath.row];
    cell.DistanceLabel.text = [distanceData objectAtIndex:indexPath.row];



    return cell;
}

我该如何解决这个错误?

【问题讨论】:

  • 哪一行导致崩溃?你在哪里说相应的委托方法中有多少行?
  • 是的,构成您的数据集的 6 个数组中的哪一个导致了问题?

标签: objective-c nsarray nsrangeexception


【解决方案1】:

您的 tableDataplaceDataXXXdata 的计数小于您单元格的计数。
您可以在这些行中设置一些断点。
cell.TitleLabel.text = [tableData objectAtIndex:indexPath.row]; cell.PlaceLabel.text = [placeData objectAtIndex:indexPath.row]; cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; cell.RankingsLabel.text = [rankingsData objectAtIndex:indexPath.row]; cell.StatusLabel.text = [statusData objectAtIndex:indexPath.row]; cell.DistanceLabel.text = [distanceData objectAtIndex:indexPath.row]; 您也可以使用tableData.count 来查看是否超出范围。

【讨论】:

  • @amuthakumari 您的一个或多个tableData placeData rankingsData statusDatadistanceData 没有足够的对象。当index参数超出数组的计数时,通常会出现此错误objectAtIndex:。例如,您有一个带有array=@[0,1,2,3] 的数组,它有4 个对象,并且您调用这样的方法array objectAtIndex:5
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
相关资源
最近更新 更多