Skip to content

Instantly share code, notes, and snippets.

@lostincode
Last active December 27, 2018 16:38
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lostincode/38e6b0a612a3b33f6f7b to your computer and use it in GitHub Desktop.
Save lostincode/38e6b0a612a3b33f6f7b to your computer and use it in GitHub Desktop.
Lighter View Controller (UICollectionViewDatasource) in Swift
//
// 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]
}
}
//
// 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...
}
}
//
// ViewController.swift
//
// Created by Bill Richards on 10/7/14.
// Copyright (c) 2014 Bill Richards. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//variable to hold reference to the datasource
var dataSource:CollectionViewDataSource?
override func viewDidLoad() {
super.viewDidLoad()
//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 {
actualCell.configureForItem(actualItem)
}
}
})
//finally, set the collectionview datasource
self.collectionView.dataSource = self.dataSource
}
}
@malshash
Copy link

malshash commented Apr 1, 2017

Agreed, thanks for implementing this in Swift for us Objective-C -> Swift learners.

@mmick66
Copy link

mmick66 commented Aug 22, 2017

Why not use a generic class rather than NSArray instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment