Swiftpack.co - Package - devxoul/ReusableKit

ReusableKit

Swift CocoaPods Build Status Codecov

Generic reusables for Cocoa. Currently supports UITableView and UICollectionView.

At a Glance

Before 🤢

collectionView.register(UserCell.self, forCellWithReuseIdentifier: "userCell")
collectionView.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! UserCell
  1. A hard-coded string identifier can cause a human error.
  2. A force downcasting should be avoided.

After 😊

let reusableUserCell = ReusableCell<UserCell>()
collectionView.register(reusableUserCell)
collectionView.dequeue(reusableUserCell) // UserCell
  1. A string identifier is generated automatically using UUID and stored in the struct.
  2. A generic can ensure the type of the dequeued cell statically.

Example Usage

It is recommended to define reusable types as a static constants in an enum or a struct.

UITableView

// 1. define
enum Reusable {
  static let headerView = ReusableCell<SectionHeaderView>()
  static let userCell = ReusableCell<UserCell>()
}

// 2. register
tableView.register(Reusable.headerView)
tableView.register(Reusable.userCell)

// 3. dequeue
tableView.dequeue(Reusable.headerView, for: indexPath)
tableView.dequeue(Reusable.userCell, for: indexPath)

UICollectionView

// 1. define
enum Reusable {
  static let headerView = ReusableCell<SectionHeaderView>()
  static let photoCell = ReusableCell<PhotoCell>()
}

// 2. register
collection.register(Reusable.headerView, kind: .header)
collection.register(Reusable.photoCell)

// 3. dequeue
collection.dequeue(Reusable.headerView, kind: .header, for: indexPath)
collection.dequeue(Reusable.photoCell, for: indexPath)

RxSwift Extension

ReusableKit supports a RxSwift extension.

users // Observable<[String]>
  .bind(to: collectionView.rx.items(Reusable.userCell)) { i, user, cell in
    cell.user = user
  }

Contrubiting

Pull requests are welcomed 💖

In order to create Xcode project, run:

$ swift package generate-xcodeproj

Installation

  • For iOS 8+ projects with CocoaPods:

    pod 'ReusableKit'
    pod 'ReusableKit/RxSwift'  # with RxSwift extension
    

License

ReusableKit is under MIT license. See the LICENSE file for more info.

Github

link
Stars: 82
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

2.0.1 - Dec 17, 2017

  • Add default subspec

2.0.0 - Sep 22, 2017

  • Add Swift 4 support ✨

1.3.0 - Jun 20, 2017

  • Add RxSwift extension (98fb6b2e09f92c9c192de149ccbcf0ebf15e15a7)

1.2.0 - Jun 20, 2017

  • Add a property class (78c88041afa82cc956c502ff8840e765e403d40c)

1.1.0 - Oct 19, 2016

  • Add support to register from nibs (#4, @devxoul)