Last active
November 23, 2016 13:27
-
-
Save olxios/119aacd77a12b7bb68e76ae6deeafe78 to your computer and use it in GitHub Desktop.
Simple UICollectionView example Swift: http://swiftiostutorials.com/tutorial-using-uicollectionview-uicollectionviewflowlayout/
This file contains hidden or 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 | |
class GalleryItem { | |
var itemImage: String | |
init(dataDictionary:Dictionary<String,String>) { | |
itemImage = dataDictionary["itemImage"]! | |
} | |
class func newGalleryItem(_ dataDictionary:Dictionary<String,String>) -> GalleryItem { | |
return GalleryItem(dataDictionary: dataDictionary) | |
} | |
} |
This file contains hidden or 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 | |
class GalleryItemCollectionViewCell: UICollectionViewCell { | |
@IBOutlet var itemImageView: UIImageView! | |
} |
This file contains hidden or 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 | |
class GalleryItemCollectionViewCell: UICollectionViewCell { | |
@IBOutlet var itemImageView: UIImageView! | |
func setGalleryItem(_ item:GalleryItem) { | |
itemImageView.image = UIImage(named: item.itemImage) | |
} | |
} |
This file contains hidden or 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 | |
class GalleryItemCommentView: UICollectionReusableView { | |
@IBOutlet var commentLabel: UILabel! | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController { | |
@IBOutlet var collectionView: UICollectionView! | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
... | |
// MARK: - UICollectionViewDataSource | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
// MARK: - UICollectionViewDataSource | |
... | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return galleryItems.count | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
// MARK: - UICollectionViewDataSource | |
... | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryItemCollectionViewCell", for: indexPath) as! GalleryItemCollectionViewCell | |
cell.setGalleryItem(galleryItems[indexPath.row]) | |
return cell | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
// MARK: - UICollectionViewDataSource | |
... | |
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { | |
let commentView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "GalleryItemCommentView", for: indexPath) as! GalleryItemCommentView | |
commentView.commentLabel.text = "Supplementary view of kind \(kind)" | |
return commentView | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
@IBOutlet var collectionView: UICollectionView! | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
... | |
// MARK: - UICollectionViewDelegate | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
let alert = UIAlertController(title: "didSelectItemAtIndexPath:", message: "Indexpath = \(indexPath)", preferredStyle: .alert) | |
let alertAction = UIAlertAction(title: "Dismiss", style: .destructive, handler: nil) | |
alert.addAction(alertAction) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
.... | |
var galleryItems: [GalleryItem] = [] // NEW PROPERTY | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
... | |
// MARK: - UICollectionViewFlowLayout | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
let picDimension = self.view.frame.size.width / 4.0 | |
return CGSize(width: picDimension, height: picDimension) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
let leftRightInset = self.view.frame.size.width / 14.0 | |
return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset) | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
.... | |
fileprivate func initGalleryItems() { | |
var items = [GalleryItem]() | |
let inputFile = Bundle.main.path(forResource: "items", ofType: "plist") | |
let inputDataArray = NSArray(contentsOfFile: inputFile!) | |
for inputItem in inputDataArray as! [Dictionary<String, String>] { | |
let galleryItem = GalleryItem(dataDictionary: inputItem) | |
items.append(galleryItem) | |
} | |
galleryItems = items | |
} | |
} |
This file contains hidden or 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 | |
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
... | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initGalleryItems() | |
collectionView.reloadData() | |
} | |
private func initGalleryItems() { | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment