使用 JSON 的 uitableview 部分的问题

Posted

技术标签:

【中文标题】使用 JSON 的 uitableview 部分的问题【英文标题】:Problems with uitableview sections using JSON 【发布时间】:2013-07-21 21:37:30 【问题描述】:

我正在创建一个用于演示目的的应用程序,并有一个用 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,所以我可能忽略了一些基本的东西。感谢您的帮助。

【问题讨论】:

首先,要么您的numberOfRowsInSection 方法返回数据源中的节数,要么您的numberOfSectionsInTableView 返回每​​个节中的行数。 请,请,请:使用下标。 json[i][@"id"] 而不是 [[json objectAtIndex:i] objectForKey:@"id"] 这与 JSON 无关。 【参考方案1】:

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

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

【讨论】:

谢谢!这很有帮助。

以上是关于使用 JSON 的 uitableview 部分的问题的主要内容,如果未能解决你的问题,请参考以下文章

iOS UITableView:使用来自 JSON 对象的信息的自定义部分,而不仅仅是计算它

如何在uitableview的单独部分中显示json子值?

我应该如何格式化带有部分的 UITableView 的传入 JSON

从 JSON 数据中对 UItableview 中的排序和部分进行排序

json 分组日期和填充 uiTableView 标题

从 JSON 解析数据直接到 UITableView