Reusable

1.0.0

iOS cell registration and reusing with generics and protocol extensions
gonzalezreal/Reusable

What's New

Reusable 1.0.0

2019-09-16T15:09:52Z

Reusable

Swift 5.1 Swift Package Manager Twitter: @gonzalezreal

Reusable is a Swift ┬Ápackage that provides a type-safe way for cell registration and dequeuing in both table and collection views. It is based on this post I wrote a few years ago while I was exploring some of the Swift language features.

Usage

To be able to register and reuse a cell or supplementary view in a type-safe way, the view must conform to the ReusableView protocol:

extension PosterItemCell: ReusableView {}

The default implementation of ReusableView will provide a reuse identifier based on the class name.

Cells implemented with Interface Builder must also conform to the NibLoadableView protocol:

extension PosterItemCell: ReusableView, NibLoadableView {}

Once the cell or supplementary view is ready, you can register it in a collection or table view by indicating its type:

func viewDidLoad() {
  super.viewDidLoad()
  collectionView.register(PosterItemCell.self)
  ...
}

Likewise, you can dequeue a cell previously registered in this way just by indicating its type:

func collectionView(_: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(PosterItemCell.self, for: indexPath)
    ...
    return cell
}

Installation

Using the Swift Package Manager

Add Reusable as a dependency to your Package.swift file. For more information, see the Swift Package Manager documentation.

.package(url: "https://github.com/gonzalezreal/Reusable", from: "1.0.0")

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to Reusable.
  • Contact @gonzalezreal on Twitter.

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Mar 14 2023 05:01:53 GMT-0500 (GMT-05:00)