Is anyone else having an issue using the tableView.registerClass method with Swift?
It no longer comes in code completion for me (nor can I use it if manual
Swift 4 and 4.1. making generic methods it is very easy to register and dequeue table cell.
override func viewDidLoad() {
super.viewDidLoad()
self.tblView.register(CellProfileOther.self) // cell class name
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CellProfileOther = tableView.dequeueReusableCell(forIndexPath: indexPath)
return cell
}
extension UITableView {
func register(_: T.Type) where T: ReusableView, T: NibLoadableView {
let bundle = Bundle(for: T.self)
let nib = UINib(nibName: T.nibName, bundle: bundle)
self.register(nib, forCellReuseIdentifier: T.defaultReuseIdentifier)
}
func dequeueReusableCell(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
guard let cell = self.dequeueReusableCell(withIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)")
}
return cell
}
}
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(describing: Self.self)
}
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(describing: Self.self)
}
}
// Here is cell class
class CellProfileOther: UITableViewCell, ReusableView, NibLoadableView {
}