Created
July 31, 2017 09:24
-
-
Save ashikahmad/8c0263cb237c92d6cb0c28ee2a95ac5e to your computer and use it in GitHub Desktop.
Makes life a little easier with UITableView or UICollectionView cells and some more :)
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
// | |
// Reusable.swift | |
// TodoMVVM | |
// | |
// Created by Ashik uddin Ahmad on 7/20/17. | |
// Copyright © 2017 Ashik uddin Ahmad. All rights reserved. | |
// | |
import UIKit | |
protocol ClassNameStringConvertible { | |
static var classNameString: String { get } | |
} | |
protocol Reusable: ClassNameStringConvertible { | |
static func reuseId()->String | |
} | |
extension ClassNameStringConvertible { | |
static var classNameString: String { | |
return "\(Self.self)" | |
} | |
} | |
extension Reusable { | |
static func reuseId()->String { | |
return classNameString | |
} | |
} | |
extension UITableView { | |
func register<T: Reusable>(_ cellType: T.Type) where T:UITableViewCell { | |
register(T.self, forCellReuseIdentifier: T.reuseId()) | |
} | |
func dequeCell<T:Reusable>()->T where T:UITableViewCell { | |
return dequeueReusableCell(withIdentifier: T.reuseId()) as! T | |
} | |
func dequeCell<T:Reusable>(for indexPath: IndexPath)->T where T:UITableViewCell { | |
return dequeueReusableCell(withIdentifier: T.reuseId(), for: indexPath) as! T | |
} | |
} | |
extension UICollectionView { | |
func dequeueCell<T:Reusable>(for indexPath: IndexPath)->T? where T:UICollectionViewCell { | |
return dequeueReusableCell(withReuseIdentifier: T.reuseId(), for: indexPath) as? T | |
} | |
} | |
extension UIStoryboard { | |
/// To use this method, VC's storyboard identifier must be the same as ClassName. | |
func instantiateVC<T: ClassNameStringConvertible>()->T where T:UIViewController { | |
return instantiateViewController(withIdentifier: T.classNameString) as! T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment