Skip to content

Instantly share code, notes, and snippets.

@paulmars
Created August 13, 2016 22:35
Show Gist options
  • Save paulmars/e04e56f2645732495b24d1a4c3355fef to your computer and use it in GitHub Desktop.
Save paulmars/e04e56f2645732495b24d1a4c3355fef to your computer and use it in GitHub Desktop.
Working collection view example in swift
//
// 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