使用JSON的uitableview部分出现问题

时间:2013-07-21 21:37:30

标签: ios json uitableview

我正在创建一个用于演示目的的应用程序,并且使用JSON数据填充了uitableview,它运行正常。现在我想根据sessionDate在表格中创建部分但我的应用程序崩溃时出现以下错误:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [Sessions objectForKey:]:无法识别的选择器发送到实例0xac2b460'

以下是解析JSON的代码:

-(void) retrieveData
{
    NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sessions-json.json" ofType:nil]];
    NSData * data = [NSData dataWithContentsOfURL:url];

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    //Set up our speakers array
    sessionsArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < json.count; i++) {
    //create sessions object
    NSString * sID = [[json objectAtIndex:i] objectForKey:@"id"];
    NSString * sStatus = [[json objectAtIndex:i] objectForKey:@"sessionStatus"];
    NSString * sDay = [[json objectAtIndex:i] objectForKey:@"sessionDay"];
    NSString * sDate = [[json objectAtIndex:i] objectForKey:@"sessionDate"];
    NSString * sTime = [[json objectAtIndex:i] objectForKey:@"sessionTime"];
    NSString * sName = [[json objectAtIndex:i] objectForKey:@"sessionName"];
    NSString * sSpeaker1 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker1"];
    NSString * sSpeaker1Company = [[json objectAtIndex:i] objectForKey:@"speaker1Company"];
    NSString * sSpeaker2 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker2"];
    NSString * sSpeaker2Company = [[json objectAtIndex:i] objectForKey:@"speaker2Company"];
    NSString * sSpeaker3 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker3"];
    NSString * sSpeaker3Company = [[json objectAtIndex:i] objectForKey:@"speaker3Company"];
    NSString * sSpeaker4 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker4"];
    NSString * sSpeaker4Company = [[json objectAtIndex:i] objectForKey:@"speaker4Company"];
    NSString * sSpeaker5 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker5"];
    NSString * sSpeaker5Company = [[json objectAtIndex:i] objectForKey:@"speaker5Company"];
    NSString * sSpeaker6 = [[json objectAtIndex:i] objectForKey:@"sessionSpeaker6"];
    NSString * sSpeaker6Company = [[json objectAtIndex:i] objectForKey:@"speaker6Company"];
    NSString * sDesc = [[json objectAtIndex:i] objectForKey:@"sessionDesc"];
    NSString * sITSCECS = [[json objectAtIndex:i] objectForKey:@"ITSCECS"];
    NSString * sSessionID = [[json objectAtIndex:i] objectForKey:@"sessionID"];

    Sessions * mySessions = [[Sessions alloc] initWithID:sID andSessionStatus:sStatus andSessionDay:sDay andSessionDate:sDate andSessionTime:sTime andSessionName:sName andSessionSpeaker1:sSpeaker1 andSpeaker1Company:sSpeaker1Company andSessionSpeaker2:sSpeaker2 andSpeaker2Company:sSpeaker2Company andSessionSpeaker3:sSpeaker3 andSpeaker3Company:sSpeaker3Company andSessionSpeaker4:sSpeaker4 andSpeaker4Company:sSpeaker4Company andSessionSpeaker5:sSpeaker5 andSpeaker5Company:sSpeaker5Company andSessionSpeaker6:sSpeaker6 andSpeaker6Company:sSpeaker6Company andSessionDesc:sDesc andITSCECS:sITSCECS andSessionID:sSessionID];

    //Add our sessions object to our sessionsArray
    [sessionsArray addObject:mySessions];


    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"sessionDate" ascending:YES];
    [sessionsArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];


    }



[self.myTableView reloadData];

}

这是numberOfSectionsInTableView中的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    //return 1;

    if (tableView == self.myTableView) {
    return self.sessionsArray.count;
    }
    else
    {
        [self searchThroughData];
        return self.results.count;
    }


}

这是numberOfRowsInSection中的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    //return sessionsArray.count;

    if (tableView == self.myTableView) {
    return self.sessionsArray.count;
    }
    else
    {
        [self searchThroughData];
        return self.results.count;
    }



}

这是titleForHeaderInSection中的代码:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [[sessionsArray objectAtIndex:section] objectForKey:@"sessionDate"];
}

我只使用Objective C几个月,所以我可能会忽略一些基本的东西。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

sessionsArray包含Sessions个对象,但是当您使用它时,您会将其视为包含字典。您还可以使用完全相同的代码来计算节数和行数。因此,看起来您打算拥有2组/级别的数据,但目前您只有1个,并且您尝试将它用于2件事。

您需要确定sessionsArray实际使用的内容并将其保留。这些部分代表什么,以及如何表示每个部分中的行。配置您的数据结构以匹配它。