Last active
June 23, 2016 10:15
-
-
Save olxios/7ed7fd5451501cfc6e2b31e6c16791f1 to your computer and use it in GitHub Desktop.
Simple UICollectionView example Objective-C: http://swiftiostutorials.com/tutorial-using-uicollectionview-uicollectionviewflowlayout/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface GalleryItem : NSObject | |
@property (nonatomic, strong) NSString *itemImage; | |
+ (instancetype)galleryItemWithDictionary:(NSDictionary *)dictionary; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GalleryItem.h" | |
@implementation GalleryItem | |
+ (instancetype)galleryItemWithDictionary:(NSDictionary *)dictionary | |
{ | |
GalleryItem *item = [[GalleryItem alloc] init]; | |
item.itemImage = dictionary[@"itemImage"]; | |
return item; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@class GalleryItem; | |
@interface GalleryItemCollectionViewCell : UICollectionViewCell | |
@property (nonatomic, weak) IBOutlet UIImageView *itemImageView; | |
- (void)setGalleryItem:(GalleryItem *)item; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GalleryItemCollectionViewCell.h" | |
#import "GalleryItem.h" | |
@implementation GalleryItemCollectionViewCell | |
- (void)setGalleryItem:(GalleryItem *)item | |
{ | |
_itemImageView.image = [UIImage imageNamed:item.itemImage]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface GalleryItemCommentView : UICollectionReusableView | |
@property (nonatomic, weak) IBOutlet UILabel *commentLabel; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@interface ViewController () | |
{ | |
NSArray *_galleryItems; // NEW PROPERTY | |
} | |
@end | |
@implementation ViewController | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@implementation ViewController | |
... | |
#pragma mark - UICollectionViewDataSource | |
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView | |
{ | |
return 1; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@implementation ViewController | |
#pragma mark - UICollectionViewDataSource | |
... | |
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section | |
{ | |
return [_galleryItems count]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> | |
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
... | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self initGalleryItems]; | |
[_collectionView reloadData]; | |
} | |
- (void)initGalleryItems | |
{ | |
.... | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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