Skip to content

Instantly share code, notes, and snippets.

@lostincode
Last active December 27, 2018 16:38

Revisions

  1. lostincode revised this gist Oct 15, 2014. 1 changed file with 16 additions and 3 deletions.
    19 changes: 16 additions & 3 deletions CustomUICollectionViewCell.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,17 @@
    //Example cell configuration
    func configureForItem(item:AnyObject) {
    //do something with the cell...
    //
    // CustomUICollectionViewCell.swift
    //
    // Created by Bill Richards on 9/24/14.
    // Copyright (c) 2014 Bill Richards. All rights reserved.
    //

    import UIKit

    class CustomUICollectionViewCell: UICollectionViewCell {

    //Example cell configuration
    func configureForItem(item:AnyObject) {
    //do something with the cell...
    }

    }
  2. lostincode revised this gist Oct 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,8 @@ class ViewController: UIViewController {
    override func viewDidLoad() {
    super.viewDidLoad()

    //setup the closure to handle our cell
    //modify AnyObject to match your model
    //Init our datasource and setup the closure to handle our cell
    //modify 'AnyObject' to match your model
    self.dataSource = CollectionViewDataSource(items: self.items, cellIdentifier: "Cell", configureBlock: { (cell, item) -> () in
    if let actualCell = cell as? CustomUICollectionViewCell {
    if let actualItem = item as? AnyObject {
  3. lostincode revised this gist Oct 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@

    import UIKit

    class ViewController: UITableViewController {
    class ViewController: UIViewController {

    //variable to hold reference to the datasource
    var dataSource:CollectionViewDataSource?
  4. lostincode revised this gist Oct 15, 2014. 1 changed file with 31 additions and 13 deletions.
    44 changes: 31 additions & 13 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,33 @@
    //variable to hold reference to the datasource
    var dataSource:CollectionViewDataSource?

    //setup the closure to handle our cell
    //modify AnyObject to match your model
    self.dataSource = CollectionViewDataSource(items: self.items, cellIdentifier: "Cell", configureBlock: { (cell, item) -> () in
    if let actualCell = cell as? CustomUICollectionViewCell {
    if let actualItem = item as? AnyObject {
    actualCell.configureForItem(actualItem)
    }
    //
    // ViewController.swift
    //
    // Created by Bill Richards on 10/7/14.
    // Copyright (c) 2014 Bill Richards. All rights reserved.
    //

    import UIKit

    class ViewController: UITableViewController {

    //variable to hold reference to the datasource
    var dataSource:CollectionViewDataSource?

    override func viewDidLoad() {
    super.viewDidLoad()

    //setup the closure to handle our cell
    //modify AnyObject to match your model
    self.dataSource = CollectionViewDataSource(items: self.items, cellIdentifier: "Cell", configureBlock: { (cell, item) -> () in
    if let actualCell = cell as? CustomUICollectionViewCell {
    if let actualItem = item as? AnyObject {
    actualCell.configureForItem(actualItem)
    }
    }
    })

    //finally, set the collectionview datasource
    self.collectionView.dataSource = self.dataSource

    }
    })

    //finally, set the collectionview datasource
    self.collectionView.dataSource = self.dataSource
    }
  5. lostincode revised this gist Oct 15, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    /*ViewController usage*/

    //variable to hold reference to the datasource
    var dataSource:CollectionViewDataSource?

  6. lostincode revised this gist Oct 15, 2014. 3 changed files with 21 additions and 0 deletions.
    File renamed without changes.
    4 changes: 4 additions & 0 deletions CustomUICollectionViewCell.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    //Example cell configuration
    func configureForItem(item:AnyObject) {
    //do something with the cell...
    }
    17 changes: 17 additions & 0 deletions ViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    /*ViewController usage*/

    //variable to hold reference to the datasource
    var dataSource:CollectionViewDataSource?

    //setup the closure to handle our cell
    //modify AnyObject to match your model
    self.dataSource = CollectionViewDataSource(items: self.items, cellIdentifier: "Cell", configureBlock: { (cell, item) -> () in
    if let actualCell = cell as? CustomUICollectionViewCell {
    if let actualItem = item as? AnyObject {
    actualCell.configureForItem(actualItem)
    }
    }
    })

    //finally, set the collectionview datasource
    self.collectionView.dataSource = self.dataSource
  7. lostincode created this gist Oct 15, 2014.
    44 changes: 44 additions & 0 deletions gistfile1.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    //
    // CollectionViewDataSource.swift
    //
    // Created by Bill Richards on 10/1/14.
    // Copyright (c) 2014 Bill Richards. All rights reserved.
    //

    import Foundation

    typealias CollectionViewCellConfigureBlock = (cell:UICollectionViewCell, item:AnyObject?) -> ()

    class CollectionViewDataSource: NSObject, UICollectionViewDataSource {

    var items:NSArray = []
    var itemIdentifier:String?
    var configureCellBlock:CollectionViewCellConfigureBlock?

    init(items: NSArray, cellIdentifier: String, configureBlock: CollectionViewCellConfigureBlock) {
    self.items = items
    self.itemIdentifier = cellIdentifier
    self.configureCellBlock = configureBlock
    super.init()
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.itemIdentifier!, forIndexPath: indexPath) as UICollectionViewCell
    let item: AnyObject = self.itemAtIndexPath(indexPath)

    if (self.configureCellBlock != nil) {
    self.configureCellBlock!(cell: cell, item: item)
    }

    return cell
    }

    func itemAtIndexPath(indexPath: NSIndexPath) -> AnyObject {
    return self.items[indexPath.row]
    }
    }