gpt4 book ai didi

ios - 使用 JSON 的 uitableview 部分出现问题

转载 作者:行者123 更新时间:2023-11-29 03:45:49 25 4
gpt4 key购买 nike

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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[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 几个月,所以我可能忽略了一些基本的东西。感谢您的帮助。

最佳答案

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

您需要确定 sessionsArray 的实际用途并保持不变。这些部分代表什么以及如何表示每个部分中的行。配置您的数据结构以匹配它。

关于ios - 使用 JSON 的 uitableview 部分出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17777188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com