TableViewCells和字典数组 [英] TableViewCells and Array of Dictionaries

查看:29
本文介绍了TableViewCells和字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何用所有日期填充表时遇到了麻烦.仅返回2012年9月1日,而不是2012年8月31日.我已经尝试了一些事情,例如在numberofrowssection中使用我的天数和广告位.我认为这与.count有关,但看不到哪里.但是我还没有弄清楚.它仍然只发布9/1字典.我如何在tableviewcell中显示所有字典数组,我希望我有道理,附有图片. 我的代码:

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];
    }
}

这是下面的样子:

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"
)}

这是我尝试过的 在.h

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;

这是一张照片

推荐答案

我认为您很亲密,因为您的数组具有所有值,而不是同时存在.尝试移动self.timesArray = [[NSMutableOrderedSet alloc] init];在for循环之前向上移动,以便仅初始化一次.另外,向下移动最终的NSLog和tableView reloadData 像这样:

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];
}

这篇关于TableViewCells和字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆