Code Monkey home page Code Monkey logo

reusablekit's Introduction

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.

reusablekit's People

Contributors

devxoul avatar murselturk avatar vilvil 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.