This extension package solves the following problems:
- Forces your team to adhere to standard practices by:
- Making storyboard identifiers the same as their class names
- Making UITableViewCell and UICollectionViewCell reuse identifiers the same as their class name
- Making UICollectionView supplementary views and UITableViewHeaderFooterView reuse identifiers the same as their class name
- Removes the need to force unwrap or force cast cells/headers as you dequeue
- Removes the need to force unwrap or force cast UIViewControllers as you instantiate them from a storyboard
If you're a user of SwiftLint, you'll greatly appreciate the reduced number of warnings and no more need for snippets in the vein of:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath) as? MyCustomCellType else {
fatalError("We didn't get the cell")
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requires that your view conforms to NibLoadableView
.
let view = MyNibLoadableView.create()
Requires that a class has the same storyboard identifier as its class name.
let myStoryboard: UIStoryboard = ...
let myCustomViewController = myStoryboard.instantiateViewControllerOfType(MyCustomViewController.self) as MyCustomViewController
Requires that a class has the same reuse identifier as its class name. You must register your cell type first.
// Registration
tableView.register(MyCustomCellType.self)
// Dequeueing
let cell = tableView.dequeueReusableCell(for: indexPath) as MyCustomCellType
Requires that a class has the same reuse identifier as its class name. You must register your cell type first.
// Registration
collectionView.register(MyCustomCellType.self)
// Dequeueing
let cell = collectionView.dequeueReusableCell(for: indexPath) as MyCustomCellType
Requires that a class has the same reuse identifier as its class name. You must register your cell type first.
// Registration
tableView.register(MyCustomHeaderFooterView.self)
/// Dequeueing
let header = tableView.dequeueReusableHeaderFooterView(inSection: section) as MyCustomHeaderFooterView
Requires that a class has the same reuse identifier as its class name. You must register your view type first.
// Registration
collectionView.register(MyCustomSupplementaryView.self, forSupplementaryViewElementOfKind: .sectionHeader) // or .sectionFooter
// Dequeueing
let view = collectionView.dequeueReusableSupplementaryView(ofKind: .sectionHeader, for: indexPath) as MyCustomSupplementaryView // also takes .sectionFooter
Views backed by nibs must implement the NibLoadableView
protocol. The protocol has a pre-defined extension, such that your view doesn't need to add any methods or properties.
class MyCoolCell: UITableViewCell, NibLoadableView {
...
}
ReusableViews is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "ReusableViews"
Hesham Salman, [email protected]
Twitter: @WhatsASoftware
ReusableViews is available under the MIT license. See the LICENSE file for more info.