GithubHelp home page GithubHelp logo

gonzalezreal / reusable Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 4.0 7 KB

iOS cell registration and reusing with generics and protocol extensions

License: MIT License

Swift 100.00%
swift protocol-extension ios collectionview tableview cell

reusable's Introduction

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.

reusable's People

Contributors

gonzalezreal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.