Last active
March 16, 2018 14:14
-
-
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.
This file contains hidden or 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
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