Create a gist now

Instantly share code, notes, and snippets.

#import <Foundation/Foundation.h>
@interface GalleryItem : NSObject
@property (nonatomic, strong) NSString *itemImage;
+ (instancetype)galleryItemWithDictionary:(NSDictionary *)dictionary;
@end
#import "GalleryItem.h"
@implementation GalleryItem
+ (instancetype)galleryItemWithDictionary:(NSDictionary *)dictionary
{
GalleryItem *item = [[GalleryItem alloc] init];
item.itemImage = dictionary[@"itemImage"];
return item;
}
@end
#import <UIKit/UIKit.h>
@class GalleryItem;
@interface GalleryItemCollectionViewCell : UICollectionViewCell
@property (nonatomic, weak) IBOutlet UIImageView *itemImageView;
- (void)setGalleryItem:(GalleryItem *)item;
@end
#import "GalleryItemCollectionViewCell.h"
#import "GalleryItem.h"
@implementation GalleryItemCollectionViewCell
- (void)setGalleryItem:(GalleryItem *)item
{
_itemImageView.image = [UIImage imageNamed:item.itemImage];
}
@end
#import <UIKit/UIKit.h>
@interface GalleryItemCommentView : UICollectionReusableView
@property (nonatomic, weak) IBOutlet UILabel *commentLabel;
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>itemImage</key>
<string>pic_1</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_2</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_3</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_4</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_5</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_6</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_7</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_8</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_9</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_10</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_11</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_12</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_1</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_2</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_3</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_4</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_5</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_6</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_7</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_8</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_9</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_10</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_11</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_12</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_1</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_2</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_3</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_4</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_5</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_6</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_7</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_8</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_9</string>
</dict>
<dict>
<key>itemImage</key>
<string>pic_10</string>
</dict>
</array>
</plist>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@end
#import "ViewController.h"
@interface ViewController ()
{
NSArray *_galleryItems; // NEW PROPERTY
}
@end
@implementation ViewController
@end
#import "ViewController.h"
@implementation ViewController
...
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
@end
#import "ViewController.h"
@implementation ViewController
#pragma mark - UICollectionViewDataSource
...
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_galleryItems count];
}
@end
#import "ViewController.h"
@implementation ViewController
#pragma mark - UICollectionViewDataSource
...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryItemCollectionViewCell" forIndexPath:indexPath];
[cell setGalleryItem:_galleryItems[indexPath.row]];
return cell;
}
@end
#import "ViewController.h"
@implementation ViewController
#pragma mark - UICollectionViewDataSource
...
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
GalleryItemCommentView *commentView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"GalleryItemCommentView" forIndexPath:indexPath];
commentView.commentLabel.text = [NSString stringWithFormat:@"Supplementary view of kind %@", kind];
return commentView;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@end
#import "ViewController.h"
...
@implementation ViewController
- (void)initGalleryItems
{
NSMutableArray *items = [NSMutableArray array];
NSString *inputFile = [[NSBundle mainBundle] pathForResource:@"items" ofType:@"plist"];
NSArray *inputDataArray = [NSArray arrayWithContentsOfFile:inputFile];
for (NSDictionary *inputItem in inputDataArray)
{
[items addObject:[GalleryItem galleryItemWithDictionary:inputItem]];
}
_galleryItems = items;
}
@end
#import "ViewController.h"
@implementation ViewController
...
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertController *controller = [UIAlertController alertControllerWithTitle: @"didSelectItemAtIndexPath:"
message: [NSString stringWithFormat: @"Indexpath = %@", indexPath]
preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle: @"Dismiss"
style: UIAlertActionStyleDestructive
handler: nil];
[controller addAction: alertAction];
[self presentViewController: controller animated: YES completion: nil];
}
@end
#import "ViewController.h"
...
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initGalleryItems];
[_collectionView reloadData];
}
- (void)initGalleryItems
{
....
}
@end
#import "ViewController.h"
@implementation ViewController
...
#pragma mark - UICollectionViewFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat picDimension = self.view.frame.size.width / 4.0f;
return CGSizeMake(picDimension, picDimension);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
CGFloat leftRightInset = self.view.frame.size.width / 14.0f;
return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment