Skip to content

Instantly share code, notes, and snippets.

@gcbrueckmann
Last active March 16, 2018 14:14
Show Gist options
  • Save gcbrueckmann/c14858f242d31095577602c378e6354d to your computer and use it in GitHub Desktop.
Save gcbrueckmann/c14858f242d31095577602c378e6354d to your computer and use it in GitHub Desktop.
A UITableView extension that allows you to dequeue reusable cells in a type safe manner.
import UIKit
extension UITableView {
// Ensures that a dequeued cell is of the expected type and fails with a meaningful message, if it is not.
func dequeueReusableCell<Cell>(ofType type: Cell.Type, withIdentifier identifier: String, for indexPath: IndexPath) -> Cell {
let cell = dequeueReusableCell(withIdentifier: identifier, for: indexPath)
guard let cellOfCorrectType = cell as? Cell else {
preconditionFailure("Dequeued cell (\(cell)) was not of expected type (\(type))")
}
return cellOfCorrectType
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment