1

Rails アプリ ( http://obscure-lake-7450.herokuapp.com/upcoming.json )から離れた JSON をアプリに入力しています。Rails アプリのすべての投稿には「リリース日」があり、リリース日ごとにグループ化されています (基本的に毎週、特定のスニーカーがリリースされます)。アプリで同じことができるようにしたいのですが、それが困難です。

私のストーリーボードには、UICollectionReusableView クラスと「releasesGroup」という識別子を持つ Collection Reusable View があり、「releasesHeader」という UICollectionReusableView に接続するラベルもあります。

また、ヘッダーに対応する日付を次のように表示したいと思います。

ここに画像の説明を入力

ビューコントローラー

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *upcomingReleaseURL = [NSURL URLWithString:@"http://obscure-lake-7450.herokuapp.com/upcoming.json"];

    NSData *jsonData = [NSData dataWithContentsOfURL:upcomingReleaseURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.upcomingReleases = [NSMutableArray array];

    NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"];

    for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
        UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
        upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:@"release_date"];
        [self.upcomingReleases addObject:upcomingRelease];
    }

    [self.collectionView registerClass:[UpcomingReleaseCell class] forCellWithReuseIdentifier:@"UpcomingReleaseCell"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.upcomingReleases count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"Cell";

    UpcomingReleaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UpcomingRelease *upcomingRelease = [self.upcomingReleases objectAtIndex:indexPath.row];

    cell.release_name.text = upcomingRelease.release_name;

    return cell;
}

私のUICollectionReusableView

@interface ReleasesGroup : UICollectionReusableView
@property (weak, nonatomic) IBOutlet UILabel *releasesHeader;
@end


@implementation ReleasesGroup
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
@end

ありがとう。

編集:

私のセグエ

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showRelease"])
    {
        NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];
        ReleaseViewController *releaseViewController = [segue destinationViewController];
        releaseViewController.singleRelease = self.upcomingReleases[selectedIndexPath.row];
    }
}

ReleaseViewController.h

#import <UIKit/UIKit.h>
#import "UpcomingRelease.h"

@interface ReleaseViewController : UIViewController

@property (strong, nonatomic) UpcomingRelease *singleRelease;
@property (weak, nonatomic) IBOutlet UILabel *release_name;
@property (weak, nonatomic) IBOutlet UILabel *release_price;
@property (weak, nonatomic) IBOutlet UILabel *release_colorway;
@property (weak, nonatomic) IBOutlet UILabel *release_date;
@property (weak, nonatomic) IBOutlet UIImageView *thumb;

@end

ReleaseViewController.m

#import "ReleaseViewController.h"

@interface ReleaseViewController ()

@end

@implementation ReleaseViewController

@synthesize singleRelease = _singleRelease;
@synthesize release_name = _release_name;
@synthesize release_price = _release_price;
@synthesize release_colorway = _release_colorway;
@synthesize release_date = _release_date;
@synthesize thumb = _thumb;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *thumbURL = nil;

    if ([_singleRelease.images isKindOfClass:[NSArray class]] && [_singleRelease.images count])
        thumbURL = [[[[[_singleRelease.images objectAtIndex:0] objectForKey:@"image_file"] objectForKey:@"image_file"] objectForKey:@"medium"] objectForKey:@"url"];

    if (thumbURL)
    {
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbURL]];
        UIImage *image = [UIImage imageWithData:imageData];
        self.thumb.image = image;
    }
    else {
        self.thumb.image = [UIImage imageNamed:@"cover.png"];
    }

    self.release_name.text = self.singleRelease.release_name;
    self.release_price.text = [NSString stringWithFormat:@"$%@", _singleRelease.release_price];
    self.release_colorway.text = self.singleRelease.release_colorway;
    self.release_date.text = [NSString stringWithFormat:@"%@", _singleRelease.formattedDate];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 に答える 1