Created
August 13, 2016 22:35
-
-
Save paulmars/e04e56f2645732495b24d1a4c3355fef to your computer and use it in GitHub Desktop.
Working collection view example in swift
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
// | |
// ViewController.swift | |
// CollectionView | |
// | |
// Created by Paul McKellar on 8/13/16. | |
// Copyright © 2016 Paul McKellar. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { | |
var collectionView: UICollectionView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let layout = UICollectionViewFlowLayout() | |
layout.itemSize = CGSizeMake(100, 100) | |
self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) | |
self.view.addSubview(collectionView) | |
self.collectionView.delegate = self | |
self.collectionView.dataSource = self | |
self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { | |
return 2; | |
} | |
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return 5; | |
} | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return 10; | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) | |
cell.backgroundColor = UIColor.redColor() | |
return cell | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment