0

***更新された質問** * *** JSONデータは、すべての週のジムクラスです。

現在、JSONデータをダウンロードしてNSObject SearchResultに保存し、ジムクラスごとに1行をUItableviewControllerに表示しています。

しかし、私はアイテム(ジムクラス)を日ごとにグループ化し、毎日自分のテーブルに表示したいと思います。

だから私の質問は、日付「DAY_OF_WEEK」でアイテムをグループ化し、毎日独自の配列(MonArray ... SunArray)に格納する必要があるかどうかです。

または、私が今行っているようにすべてのデータを収集し、毎日のグループに配列を並べ替えますか?

これが理にかなっていることを願っています。

ご協力いただきありがとうございます 。

- (void)viewDidLoad {

[super viewDidLoad];

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: JsonURL];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES]; });  }

-(SearchResult *)parseTrack:(NSDictionary *)dictionary {
SearchResult *searchResult1 = [[SearchResult alloc] init];
searchResult1.day = [[dictionary objectForKey:@"post"] objectForKey:@"DAY_OF_WEEK"];
searchResult1.classType= [[dictionary objectForKey:@"post"] objectForKey:@"CLASS_TYPE"];
return searchResult1;
}


- (void)fetchedData:(NSData *)responseData { //parse out the json data

searchResults2 = [NSMutableArray arrayWithCapacity:10];

NSError* error;
NSDictionary* dictionary = [NSJSONSerialization
                      JSONObjectWithData:responseData //1 
                            options:kNilOptions error:&error];
NSArray *array = [dictionary objectForKey:@"posts"];

NSLog(@"array : %@",array);

if (array == nil) {
    NSLog(@"Expected 'posts' array");
    return;
}
for (NSDictionary *resultDict in array) {

    SearchResult *searchResult3;

    searchResult3 = [self parseTrack:resultDict];

    if (searchResult3 != nil) {
        [searchResults2 addObject:searchResult3];
    }

    NSLog(@"day: %@, class: %@", [[resultDict objectForKey:@"post"] objectForKey:@"DAY_OF_WEEK"], [[resultDict objectForKey:@"post"] objectForKey:@"CLASS_TYPE"]);

}

[self.tableView reloadData];


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


 #pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {

// Return the number of sections.
return 1;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

// Return the number of rows in the section.
return searchResults2.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

SRCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SRCell1"];


// Configure the cell...


SearchResult *searchResult1 = [searchResults2 objectAtIndex:indexPath.row];
cell.daynameLabel.text = searchResult1.day;

return cell;
}

JSON出力NSlog

2012-04-26 06:12:51.256 passingdata[91699:fb03] array : (
    {
    post =         {
        "CLASS_LEVEL" = "Intro/General";
        "CLASS_TYPE" = "Muay Thai";
        "DAY_OF_WEEK" = Friday;
        ID = 19;
        "ORDER_BY" = 5;
        TIME = "1:00pm - 2:30pm";
    };
}
    {
    post =         {
        "CLASS_LEVEL" = "General/Intermediate/Advanced";
        "CLASS_TYPE" = "Muay Thai Spar - Competitive";
        "DAY_OF_WEEK" = Friday;
        ID = 27;
        "ORDER_BY" = 5;
        TIME = "6:00pm - 9:00pm";
    };
},
    {
    post =         {
        "CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
        "CLASS_TYPE" = "Fighters Training";
        "DAY_OF_WEEK" = Monday;
        ID = 1;
        "ORDER_BY" = 1;
        TIME = "9:30am - 11:00pm";
    };
},
4

1 に答える 1

2
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DAY_OF_WEEK" ascending:TRUE];
[sourceArr sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];

これがあなたを助けますように。

于 2012-04-26T06:29:02.710 に答える