Drag out Single View Controller. Add a CollectionView.

Screen Shot 2017-06-04 at 6.03.52 AM.png

Pin it to the walls.

Screen Shot 2017-06-04 at 6.04.36 AM.png

Implement the interfaces

Screen Shot 2017-06-04 at 6.05.36 AM.png

The create an data struct and fill it with data.

Screen Shot 2017-06-04 at 6.08.33 AM.png

Create a UICollectionViewCell.

Screen Shot 2017-06-04 at 6.10.25 AM.png

Add a label to the cell in the story board.

Screen Shot 2017-06-04 at 6.11.52 AM.png

Connect the cell in the storyboard to the UICollectionViewCell we just created.

Screen Shot 2017-06-04 at 6.14.36 AM.png

Give it an id.

Screen Shot 2017-06-04 at 6.14.50 AM.png

Hook it up to an IBOutlet.

Screen Shot 2017-06-04 at 6.15.47 AM.png

Instantiate and populate the cell in the delegate methods.

Screen Shot 2017-06-04 at 6.20.11 AM.png

Add the CollectionView as a property.

Screen Shot 2017-06-04 at 6.23.15 AM.png

Make yourself the delegate.

Screen Shot 2017-06-04 at 6.24.53 AM.png

Set yourself as the DataSource by control dragging from CollectionView to yellow ViewController in storyboard.

Screen Shot 2017-06-04 at 6.28.49 AM.png

Should now run and populate.

Screen Shot 2017-06-04 at 6.29.53 AM.png

To make the flow horizontal change the flow.

Screen Shot 2017-06-04 at 6.30.40 AM.png

And to make rows selectable implement.

Screen Shot 2017-06-04 at 6.34.40 AM.png

Which will print out.

Screen Shot 2017-06-04 at 6.32.43 AM.png

Optionally you can also implement these following methods for more layout control.

Screen Shot 2017-06-04 at 6.33.27 AM.png

Code

TrackCell.h

#import

@interface TrackCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *name;
@end

TrackCell.m

#import "TrackCell.h"
@implementation TrackCell
@end

ViewController.m

#import "ViewController.h"
#import "TrackCell.h"

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *tracks;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tracks = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil];

    self.collectionView.delegate = self;
}

#pragma mark - UICollectionViewDataSource

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

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

    TrackCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.name.text = (NSString *)self.tracks[indexPath.row];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@", self.tracks[indexPath.row]);
}

@end

Advertisement