我正在创建一个用于演示目的的应用程序,并有一个用 JSON 数据填充的 uitableview,它工作正常。现在我想根据 sessionDate 在表中创建部分,但我的应用程序崩溃并出现以下错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Sessions objectForKey:]: unrecognized selector sent to instance 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 几个月,所以我可能忽略了一些基本的东西。谢谢你的帮助。