Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgmoose/b7342256c86a3f078caa to your computer and use it in GitHub Desktop.
Save vgmoose/b7342256c86a3f078caa to your computer and use it in GitHub Desktop.

Snippet 1

goes in CVDCollectionViewController.m, replaces existing viewDidLoad

- (void)viewDidLoad
{
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    
    // color the CollectionView's background color
    self.collectionView.backgroundColor = [UIColor colorWithHue:.5 saturation:.3 brightness:.4 alpha:1];
}

Snippet 2

The below import goes in CVDCollectionViewController.m. It is required so that we can create custom cells from our view controller.

#import "CVDCollectionViewCell.h"

Snippet 3

This snippet goes in CVDCollectionViewController.m. It is one of the mandatory methods to implement along with numberOfSectionsInCollectionView and numberOfItemsInSection.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CVDCollectionViewCell* cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
                                              forIndexPath:indexPath];
    NSLog(@"Current index: %@", indexPath);
    
    return cell;
}

Snippet 4

goes in CVDCollectionViewController.m. These are the methods that are required for a dataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

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

##Snippet 5 These are the array initializations that go in CVDCollectionViewController.h

@interface CVDCollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSArray* _labelArray;
}
@end

Snippet 6

This is the snippet of the array that we used in our demo app. You can feel free to replace it with your own. These go in ViewController.m inside of viewDidLoad (after background setting).

    _labelArray = [[NSArray alloc]initWithObjects:@"Terence", @"Bomb", @"Luca", @"Willow", @"Matilda", @"Red", @"Chuck", @"Stella", @"Dahlia", @"Poppy", @"Bubbles", @"The-Blues", nil];

Snippet 7

This snippet is the output of our control+drag from the UILabel to the CVDCollectionViewController.h file. You will not be able to paste this, but you can use it as a reference.

@property (weak, nonatomic) IBOutlet UILabel *_label;
@end

Snippet 8

Replace the NSLog line in Snippet 3 with the below line in CVDCollectionViewController.m. It will set the text label for the current cell with the appropriate position in the array.

    [[cell _label]setText:[_labelArray objectAtIndex:indexPath.item]];

Snippet 9

This snippet will replace the code from Snippet 4. It is necessary to display the proper number of cells in our labelArray.

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

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

Snippet 10

This is just to confirm what the dragging event form the UIImageView will look like. You cannot paste this one.

@property (weak, nonatomic) IBOutlet UIImageView *_image;

Snippet 11

This line goes after the setText line from Snippet 8. It connects the images from the array to the cell.

[[cell _image]setImage:[UIImage imageNamed:[NSString stringWithFormat:[_labelArray objectAtIndex:indexPath.item], @".png"]]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment