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

时间:2014-12-10 06:38:05

标签: ios arrays uitableview

我是iOS新手。我正在制作一个应用程序,我得到了json的响应,所有工作正常。我将响应存储在一个数组中,但是当我将数组值设置为tableview单元时,就像这样

valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"]

它给了我一个错误的例外。提前谢谢。下面是我的示例代码。

-(void)viewdidload{
  NSMutableDictionary *callDict =[[NSMutableDictionary alloc] init];
    [callDict setObject:@"messages-getModuleMessages" forKey:@"call"];
    [callDict setObject:FB_API_KEY forKey:@"accessSecret"];
    NSString *x=[FBUserManager sharedUserManager].authToken;
    [callDict setObject:x forKey:@"authToken"];
    [callDict setObject:@"json" forKey:@"format"];
    [callDict setObject:@"inbox" forKey:@"callType"];

    FBGenericWebHandler *handler = [[FBGenericWebHandler alloc] init];
    handler.delegate = self;
    [handler Inboxmessages:callDict];
    handler = nil;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];



}

我正在收到这样的回复

-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    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;
}

2 个答案:

答案 0 :(得分:2)

它直截了当,因为错误说"' NSRangeException',原因: - [__ NSArrayM objectAtIndex:]:索引0超出空数组的界限'",你的数组为空并且您正在尝试访问其第0个(即第一个元素)。

您可以执行的操作是在检索值之前进行检查:if ([yourArray count] > 0){....}

if (self.inboxmessagesarray.count > 0){
    valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"]
}

答案 1 :(得分:1)

你可能有self.inboxmessagesarray.count + 1(加1更加加载)innumberOfRowsInSection:方法,当self.inboxmessagesarray.count == 0那么你是valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"]; 这引发了这个异常

相关问题