Skip to content

Instantly share code, notes, and snippets.

@leilee
Created September 19, 2016 03:05
Show Gist options
  • Select an option

  • Save leilee/ac5023fb830734494b2455fcb1e43d1e to your computer and use it in GitHub Desktop.

Select an option

Save leilee/ac5023fb830734494b2455fcb1e43d1e to your computer and use it in GitHub Desktop.
import Foundation
// MARK: tableview
extension UITableView {
func dequeueReusableCellWithIndexPath<T: UITableViewCell where T: TableViewCellIdentifiable>(indexPath: NSIndexPath) -> T {
guard let cell = self.dequeueReusableCellWithIdentifier(T.cellIdentifier, forIndexPath: indexPath) as? T else {
fatalError("Couldn't instantiate view controller with identifier \(T.cellIdentifier) ")
}
return cell
}
}
// MARK: UITableViewCell
protocol TableViewCellIdentifiable {
static var cellIdentifier: String { get }
}
extension TableViewCellIdentifiable where Self: UITableViewCell {
static var cellIdentifier: String {
return String(self)
}
}
extension UITableViewCell : TableViewCellIdentifiable { }
// MARK: storyboard
extension UIStoryboard {
enum Storyboard: String {
case Main
case Enterprise
case Collaborators
}
convenience init(storyboard: Storyboard, bundle: NSBundle? = nil) {
self.init(name: storyboard.rawValue, bundle: bundle)
}
class func storyboard(storyboard: Storyboard, bundle: NSBundle? = nil) -> UIStoryboard {
return UIStoryboard(name: storyboard.rawValue, bundle: bundle)
}
func instantiateViewController<T: UIViewController where T: StoryboardIdentifiable>() -> T {
guard let viewController = self.instantiateViewControllerWithIdentifier(T.storyboardIdentifier) as? T else {
fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier) ")
}
return viewController
}
}
extension UIViewController : StoryboardIdentifiable { }
// MARK: identifiable
protocol StoryboardIdentifiable {
static var storyboardIdentifier: String { get }
}
extension StoryboardIdentifiable where Self: UIViewController {
static var storyboardIdentifier: String {
return String(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment