🎏 ReusableProtocol is a helpful and pure Swift implemented library for registering and reusing cells or views in the table view and collection view. You may never care about what the reuse identifier is.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 8.0+
ReusableProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ReusableProtocol'
-
Register the cell or view:
// UITableView tableView.register(TableViewCell.self) tableView.register(TableViewHeaderView.self) // UICollectionView collectionView.register(CollectionViewCell.self) collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader)
-
Dequeue the cell or view:
// UITableView let cell = tableView.dequeueReusableCell(with: TableViewCell.self, for: indexPath) let view = tableView.dequeueReusableHeaderFooterView(with: TableViewHeaderView.self) // UICollectionView let cell = collectionView.dequeueReusableCell(with: CollectionViewCell.self, for: indexPath) let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, viewClass: CollectionHeaderView.self, for: indexPath)
-
Optional: You may want to register the cell or view from nib:
// UITableView tableView.register(TableViewCell.self, fromNib: true) tableView.register(TableViewHeaderView.self, fromNib: true) // UICollectionView collectionView.register(CollectionViewCell.self, fromNib: true) collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, fromNib: true)
Leo, [email protected]
ReusableProtocol is available under the MIT license. See the LICENSE file for more info.