Created
September 26, 2020 16:11
-
-
Save erdemildiz/a9c9d794d3aac8267bcb1932eedf673e to your computer and use it in GitHub Desktop.
UITableView+Common
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
#https://github.com/sgl0v/TMDB/blob/master/TMDB/Sources/Screens/Utils/UITableView%2BCommon.swift | |
import UIKit | |
protocol NibProvidable { | |
static var nibName: String { get } | |
static var nib: UINib { get } | |
} | |
extension NibProvidable { | |
static var nibName: String { | |
return "\(self)" | |
} | |
static var nib: UINib { | |
return UINib(nibName: self.nibName, bundle: nil) | |
} | |
} | |
protocol ReusableView { | |
static var reuseIdentifier: String { get } | |
} | |
extension ReusableView { | |
static var reuseIdentifier: String { | |
return "\(self)" | |
} | |
} | |
// Cell | |
extension UITableView { | |
func registerClass<T: UITableViewCell>(cellClass `class`: T.Type) where T: ReusableView { | |
register(`class`, forCellReuseIdentifier: `class`.reuseIdentifier) | |
} | |
func registerNib<T: UITableViewCell>(cellClass `class`: T.Type) where T: NibProvidable & ReusableView { | |
register(`class`.nib, forCellReuseIdentifier: `class`.reuseIdentifier) | |
} | |
func dequeueReusableCell<T: UITableViewCell>(withClass `class`: T.Type) -> T? where T: ReusableView { | |
return self.dequeueReusableCell(withIdentifier: `class`.reuseIdentifier) as? T | |
} | |
func dequeueReusableCell<T: UITableViewCell>(withClass `class`: T.Type, forIndexPath indexPath: IndexPath) -> T where T: ReusableView { | |
guard let cell = self.dequeueReusableCell(withIdentifier: `class`.reuseIdentifier, for: indexPath) as? T else { | |
fatalError("Error: cell with identifier: \(`class`.reuseIdentifier) for index path: \(indexPath) is not \(T.self)") | |
} | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment