【问题标题】:Pull to refresh resulting app to crash: NSRangeException, reason: index 8 beyond bounds [0 .. 4]拉动刷新导致应用程序崩溃:NSRangeException,原因:索引 8 超出范围 [0 .. 4]
【发布时间】:2016-09-26 02:47:05
【问题描述】:

抱歉,之前有人问过这个问题,但我找不到问题的答案。

我编写了用于拉取刷新的代码,但如果结果数组计数与indexPath.row 不同,它就会崩溃,并且我试图删除我的数组对象但它仍然崩溃。

我的代码是这样的:

-(void)loadMore:(id) sender {
    loadFlag = 1;
    NSString *employeeID = @"aaa";
    days = 3;

    int projIDNum = 1234;
    int depCode = 1234000001;
    int caseNum = 45678;
    NSString *result = [client report_search:employeeID case_no:caseNum status:loadFlag 
        day:days project_id:projIDNum dep_code:depCode count:10];
    if ([result isEqualTo:@"[]"]) {
        [Utilities callAlert:self title:@"No More Data" body:@"There is no other data contains"];
        [refresh endRefreshing];
    } else {
        NSData *jsonData = [result dataUsingEncoding: NSUTF8StringEncoding];
        NSError *error;
        [resultArr removeAllObjects];
        // resultArr is NSMutableArray and it's the tableview's data source
        resultArr = [[NSJSONSerialization JSONObjectWithData:jsonData options: 0 
            error:&error] mutableCopy];
        [refresh endRefreshing];
        [_mSearchTableView reloadData];
    }
}

如果我得到完整的 10 个数据结果,程序本身不会崩溃,但是当我从 Web 服务获得少于 10 个数据时会发生这种情况。我不断收到这个异常,NSRangeException, reason: index 8 beyond bounds [0 .. 4],但不知道如何处理它。

我的表格视图设置:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger) table:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [resultArr count];
}

这是我的 cellForRowAtIndexPath – Tj3n

-(UITableViewCell) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // clear cell
    for ([UIView *view in [cell.contentView subviews]]) {
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }

    UILabel *titleLabel = [[UILabel alloc] init];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    } else {
        titleLabel = [cell.contentView viewWithTag:100];
    }

    CGRect location = CGRectMake(20, 20, 71/75, 40/146);
    titleLabel = [[UILabel alloc] initWithFrame:location];
    titleLabel.textAlignment = NSTextAlignmentNatural;
    titleLabel.text = [resultArr valueForKey:@"title"][indexPath.row];  // this is the line always crashed
    [cell.contentView addSubview:titleLabel];

    return cell;
}

同时更新我的​​ json:

print out result: [
    {
    Content = "Test\nTest\n";
    "Create_Datetime" = "2016-09-12 10:18:25";
    "Project_ID" = 2023;    
    Status = 4;    
    "CASE_NO" = 17704;
    Title = "Table title";
    "Update_Datetime" = "2016-09-12 10:19:09";
},
    {
    Content = "Fhujhfdfhjoookbvv\nGu...";
    "Create_Datetime" = "2016-09-12 10:09:28";
    "Project_ID" = 2023;
    Status = 4;
    "CASE_NO" = 17703;
    Title = "Table title";
    "Update_Datetime" = "2016-09-12 10:12:59";
},
    {
    Content = dafsdfasfasfas;
    "Create_Datetime" = "2016-09-04 01:52:09";
    "Project_ID" = 2023;
    Status = 5;
    "CASE_NO" = 16512;
    Title = "Table title";
    "Update_Datetime" = "2016-09-14 16:44:46";
},
    {
    Content = dafsdfa;
    "Create_Datetime" = "2016-09-04 01:41:59";
    "Project_ID" = 2023;
    Status = 4;
    "CASE_NO" = 16511;
    Title = "Table title";
    "Update_Datetime" = "2016-09-04 01:42:23";
},
    {
    Content = sdfasfasf;
    "Create_Datetime" = "2016-09-02 14:14:39";
    "Project_ID" = 2023;
    Status = 2;
    "CASE_NO" = 16502;
    Title = "Table title Table subtitle";
    "Update_Datetime" = "2016-09-02 15:17:50";
}]

更新: 我尝试在崩溃前打印出不同位置的数组计数日志。

第一位是before table reload,数组中有5个对象。

第二名是cellForRowsAtIndexpath。我有 2 个项目要打印出来

  1. resultArr的总数为5,与数组一致 重新加载前计数。
  2. 当前的 indexpath.row。这里发生的奇怪事情是它在第 8 格而不是第 5 格

最后,我打印日志的地方是numberOfRowsInSection。在应用崩溃之前,它不会打印出日志。

【问题讨论】:

  • 发布您的cellForRowAtIndexPath
  • 更新已发布
  • 你不需要清除单元格但看起来很正常,你可以设置断点并查看导致崩溃的行吗?
  • 对不起,我不清楚我的描述titleLabel.text = [resultArr valueForKey:@"title"][indexPath.row]; 这行是导致崩溃的地方。我不断得到我的 indexPath.row 总共有 0 .. 8 但实际上它应该是 0 .. 4
  • 您在该行中得到了错误的项目,这就是导致崩溃的原因,[resultArr valueForKey:@"title"] 是一个数组吗?

标签: ios objective-c uitableview pull-to-refresh


【解决方案1】:

我认为您获取标题的方式有问题,而不是 titleLabel.text = [resultArr valueForKey:@"title"][indexPath.row];,您能否尝试以下操作。如果这不起作用,您能否打印出每个值的值,即“字典”和“标题”的值是什么。

NSDictionary *dictionary = [resultArr objectAtIndex:indexPath.row];
NSString *title = [dictionary objectForKey:@"Title"]; 
titleLabel.text = title;

【讨论】:

  • 很抱歉这么晚才回复...我还有其他业务需要先处理。抱歉我不能透露更多,因为我在做商业项目,我不想透露太多细节。我用你的方法试过了,还是崩溃了
  • 别担心,你能检查一下 [resultArr objectAtIndex:indexPath.row];是字典而 [dictionary objectForKey:@"Title"] 是字符串?如果你使用我的三行,它会在哪一行崩溃?
  • 它以同样的原因在第一行崩溃。我觉得tableviewcell 创建的不仅仅是数组,但我找不到它的原因
  • 如果你在第一行设置断点,indexpath.row 是 8 或者它基本上是大于 4 的值?您之前可能也说过,numberOfRowsForSection 是否始终为 4,设置断点并检查。您也可以尝试使用 'dequeueReusableCellWithIdentifier:' 而不是 'dequeueReusableCellWithIdentifier:forIndexPath:'
  • 是的,它确实是一本字典,当我可以提取字典中的内容时。关于这个奇怪错误的另一件事是它并非一直都在发生。仅当我从 Web 服务加载剩余数据时才会发生这种情况
【解决方案2】:

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'

感谢 JingJingTao 和 Tj3n 帮助我。

我终于找到了这个错误的答案,但我仍然无法弄清楚这个错误的原因和原因。从逻辑上讲,它应该先到numberOfRowsInSection 来设置当前表的行数,但显然它没有。有人能解释一下这是为什么吗?

【讨论】:

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