拉动以刷新生成的应用程序崩溃:NSRangeException,原因:索引8超出边界[0 .. 4]

时间:2016-09-26 02:47:06

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

很抱歉,如果之前有人询问,但我找不到问题的答案。

我编写了用于刷新的代码,但是如果结果数组计数与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]但不知道如何处理它。

我的tableview设置:

-(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。我有两个要打印的项目

  1. resultArr的总计数为5,由数组组成 在重新加载之前计算。
  2. 当前的indexpath.row。在这里发生的奇怪的事情是它在第8个单元而不是第5个
  3. 最后,我打印日志的地方是numberOfRowsInSection。在应用程序崩溃之前,它不会打印出日志。

2 个答案:

答案 0 :(得分:0)

我认为如何获得标题会出现问题,而不是titleLabel.text = [resultArr valueForKey:@"title"][indexPath.row];,您可以尝试以下方法。如果这不起作用,你能打印出每个值的价值,即'字典的价值是什么?和' title'是

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

答案 1 :(得分:0)

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

谢谢JingJingTao和Tj3n帮助我。

我终于找到了这个bug的答案,但我仍然无法找出原因和原因。从逻辑上看,它应首先转到numberOfRowsInSection来设置当前表的行数,但显然它没有。有人可以解释为什么会这样吗?

相关问题