Last active
December 27, 2018 16:38
-
-
Save lostincode/38e6b0a612a3b33f6f7b to your computer and use it in GitHub Desktop.
Lighter View Controller (UICollectionViewDatasource) in Swift
This file contains 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
// | |
// 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] | |
} | |
} |
This file contains 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
// | |
// 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... | |
} | |
} |
This file contains 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 | |
// | |
// 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 | |
} | |
} |
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
Agreed, thanks for implementing this in Swift for us Objective-C -> Swift learners.