Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Created September 26, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erdemildiz/a9c9d794d3aac8267bcb1932eedf673e to your computer and use it in GitHub Desktop.
Save erdemildiz/a9c9d794d3aac8267bcb1932eedf673e to your computer and use it in GitHub Desktop.
UITableView+Common
#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