Skip to content

Instantly share code, notes, and snippets.

@matux
Last active May 16, 2016 03:47
Show Gist options
  • Select an option

  • Save matux/86f25b97a0d6ca097bf139589fec580f to your computer and use it in GitHub Desktop.

Select an option

Save matux/86f25b97a0d6ca097bf139589fec580f to your computer and use it in GitHub Desktop.
import UIKit
/**
Describes a class that's a *ViewCell such as UICollectionViewCell or UITableViewCell.
*/
public protocol ReusableViewCell: class {
init()
}
/**
Factory for instantiating ReusableViewCells with its most specifc type. It's used for scripts generate code
that captures a View Cell subtype and it's reuseIdentifier in storyboards.
*/
public struct ViewCellFactory<T: ReusableViewCell> {
/// The reuse identifier of the cell in its storyboard.
public let reuseIdentifier: String
/**
Initialize a factory for a view cell from a storyborad.
- Parameter reuseIdentifier: The reuseIdentifier of the cell in its storyboard.
*/
public init(_ reuseIdentifier: String) {
self.reuseIdentifier = reuseIdentifier
}
}
public extension ViewCellFactory where T: UICollectionViewCell {
/**
Dequeue a cell for reuse from a UICollectionView.
- Parameter table: The table view to dequeue the cell from.
- Returns: A table view cell with it's type specified at this value's creation.
*/
public func dequeue(fromCollectionView collection: UICollectionView,
forIndexPath indexPath: NSIndexPath) -> T
{
return collection.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier,
forIndexPath: indexPath) as? T ?? T()
}
}
public extension ViewCellFactory where T: UITableViewCell {
/**
Dequeue a cell for reuse from a UITableView.
- Parameter table: The table view to dequeue the cell from.
- Returns: A table view cell with it's type specified at this value's creation.
*/
public func dequeue(fromTableView table: UITableView) -> T {
return table.dequeueReusableCellWithIdentifier(self.reuseIdentifier) as? T ?? T()
}
}
extension UICollectionViewCell: ReusableViewCell { }
extension UITableViewCell: ReusableViewCell { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment