由于未捕获的异常终止应用程序' NSRangeException',原因: - [__ NSArrayM objectAtIndex:]:索引0超出空数组的范围' - 2

时间:2014-12-10 08:02:25

标签: ios json uitableview

作为json中的新人工作我想在tableview单元格中显示数据,但是当我填充我的表格视图单元格时,它会给出上面提到的异常。我以前曾问过这个问题,但没有得到确切的解决方案 我正在收到像这样的响应加载

-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
{
    NSLog (@"Count %d", [[response objectForKey:@"messageResponse"] length] );
    NSMutableArray *inboxArray = [[NSMutableArray alloc] init];
   NSArray *firstarray=[[[response objectForKey:@"messageResponse"] objectAtIndex:1] objectForKey:@"messages"];
    for(NSDictionary *tmp in firstarray)
    {
        NSMutableDictionary *messages=[[NSMutableDictionary alloc]init];
        [messages setValue:[tmp objectForKey:@"messageId"] forKey:@"messageId"];
//        [self.inboxmessagesarray addObject:messages];




    }
    [self.activitiesTableView_ reloadData];







}

但是当我填充我的单元格时

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

    static NSString *tableviewidentifier = @"cell";
    tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];

    if(cell==nil)
    {
        cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
    }if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
        // [[cell textLabel] setText:@"Load more records"];
    }

    UILabel *valuedate = (UILabel *)[cell viewWithTag:21];
    UILabel *msg = (UILabel *)[cell viewWithTag:22];
    UILabel *date = (UILabel *)[cell viewWithTag:23];
    UILabel *time = (UILabel *)[cell viewWithTag:24];
   valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"];
    return cell;
}

它给出了一个例外。提前几天,他们确实坚持了很多天。

3 个答案:

答案 0 :(得分:2)

将您的self.lblArray 循环更改为 inboxmessagesGetSuccess 方法

-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
{
  NSMutableArray *inboxArray = [[NSMutableArray alloc] init];
  NSArray *firstarray=[[[response objectForKey:@"messageResponse"] objectAtIndex:1] objectForKey:@"messages"];
  for(NSDictionary *tmp in firstarray)
  {
    NSMutableDictionary *messages=[[NSMutableDictionary alloc]init];
    [messages setValue:[tmp objectForKey:@"messageId"] forKey:@"messageId"];
    [self.inboxmessagesarray addObject:messages];
  }

    for (int i=0; i<self.inboxmessagesarray.count; i++)
  {
    [self.lblArray addObject:[NSString stringWithFormat:@"cell Number %d",i]];
  }

  [self.activitiesTableView_ reloadData];
}

答案 1 :(得分:1)

试试这个

-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
    {
        if(response && response[@"messageResponse"])
        {
            NSArray *messageResponseList = response[@"messageResponse"];
            if(messageResponseList && [messageResponseList count]>=1)
            {
                NSLog (@"Count %lu", (unsigned long)[messageResponseList count]);
                NSMutableArray *inboxArray = [[NSMutableArray alloc] init];
                NSDictionary *message = [messageResponseList objectAtIndex:1];
                if(message && message[@"messages"])
                {
                    NSArray *firstarray=[[messageResponseList objectAtIndex:1] objectForKey:@"messages"];
                    for(NSDictionary *tmp in firstarray)
                    {
                        NSMutableDictionary *messages=[[NSMutableDictionary alloc]init];
                        [messages setValue:[tmp objectForKey:@"messageId"] forKey:@"messageId"];
                        [self.inboxmessagesarray addObject:messages];
                    }
                }
            }

        }

         [self.activitiesTableView_ reloadData];
    }

答案 2 :(得分:0)

而不是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.lblArray.count;

}

你应该有:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.inboxmessagesarray.count;

}

就像现在一样,你的tableview尝试在应用程序加载之前填充 - (void)inboxmessagesGetSuccess:(FBGenericWebHandler *)处理程序响应:(NSDictionary *)响应方法被调用,所以此时你的inboxmessagesarray是空的,但你的datasource告诉uitableview它应该有lblArray.count单元格,并且该数组不是空的。

相关问题