TableViewCells and Array of Dictionaries

扶醉桌前 提交于 2019-12-12 01:41:10

问题


I am having trouble understanding how to populate my table with all the dates. returns only the day 9-1-2012 not 8-31-2012. I have tried a few things like using my days and slots in numberofrowssection. I am thinking it has something to do with .count but am not seeing where. But I have not figured it out yet. It still is only posting 9/1 dictionary. How do I display all the array of dictionaries in tableviewcell, I hope I am making sense, picture attached. My code:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

    NSDictionary* myslots =[json objectForKey:@"slots"];
    NSLog(@"allslots: %@", myslots);

    for (NSString *slotKey in myslots.allKeys) {
    NSDictionary *slot = [myslots valueForKey:slotKey];
       UPDATED:
        self.timesArray = [[NSMutableOrderedSet alloc] init];
    for (NSDictionary *myDays in slot){

        if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]])

           [self.timesArray addObject:[myDays objectForKey:@"begin"]];
         //NSLog(@"This is the times count: %@", timesArray.count);
    }

       NSLog(@"These are the times avail: %@", self.timesArray);          

    [self.tableView reloadData];
    }
}

Here is what that looks like:

2012-08-31 16:34:57.074 GBSB[780:15b03] These are the times avail: {(
    "2012-08-31 09:00:00 America/Los_Angeles",
    "2012-08-31 13:00:00 America/Los_Angeles",
    "2012-08-31 14:00:00 America/Los_Angeles",
    "2012-08-31 15:00:00 America/Los_Angeles",
    "2012-08-31 16:00:00 America/Los_Angeles"
)}
2012-08-31 16:34:57.074 GBSB[780:15b03] These are the times avail: {(
    "2012-09-01 09:00:00 America/Los_Angeles",
    "2012-09-01 10:00:00 America/Los_Angeles",
    "2012-09-01 11:00:00 America/Los_Angeles",
    "2012-09-01 12:00:00 America/Los_Angeles",
    "2012-09-01 13:00:00 America/Los_Angeles",
    "2012-09-01 14:00:00 America/Los_Angeles",
    "2012-09-01 15:00:00 America/Los_Angeles",
    "2012-09-01 16:00:00 America/Los_Angeles"
)}

Here is what I have tried in .h

@property (nonatomic, retain) NSMutableOrderedSet *timesArray;

.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.timesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    //cell.textLabel.text = timesArray;
    cell.textLabel.text = [self.timesArray objectAtIndex:indexPath.row];
    return cell;

Here is a pic


回答1:


I think you are close, because your array has all of the values, just not at the same time. Try moving self.timesArray = [[NSMutableOrderedSet alloc] init]; up before the for loop so that it is only initialized once. Also, move the final NSLog and tableView reloadData down like this:

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

    NSDictionary* myslots =[json objectForKey:@"slots"];
    self.timesArray = [[NSMutableOrderedSet alloc] init];
    NSLog(@"allslots: %@", myslots);

    for (NSString *slotKey in myslots.allKeys) {
        NSDictionary *slot = [myslots valueForKey:slotKey];
       UPDATED:
        for (NSDictionary *myDays in slot){

            if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]])

               [self.timesArray addObject:[myDays objectForKey:@"begin"]];
            //NSLog(@"This is the times count: %@", timesArray.count);
        }

    }
    NSLog(@"These are the times avail: %@", self.timesArray);
    [self.tableView reloadData];
}


来源:https://stackoverflow.com/questions/12232385/tableviewcells-and-array-of-dictionaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!