Giter Club home page Giter Club logo

kasayon's Introduction

Kasayon

Kasayon, which apparently means convenience in Cuban - is a small collection of protocols and and extensions that aims to make working with table- and collectionviews slightly more enjoyable.

ReusableView

Both UITableViewCell and UICollectionViewCell conform to ReusableView out of the box and hence expose a static property named reuseIdentifier.

/// ReusableView can be used with any view that is reusable
public protocol ReusableView: class {
    static var reuseIdentifier: String { get }
}

public extension ReusableView where Self: UIView {
    static var reuseIdentifier: String {
        return String(describing: self)
    }
}

NibLoadable

The NibLoadable protocol is very similar to the ReusableView protocol, but it exposes a static property named nibName.

/// NibLoadable is used with classes that can be loaded via a nib with the same name
public protocol NibLoadable: class {
    static var nibName: String { get }
}

public extension NibLoadable where Self: UIView {
    static var nibName: String {
        return String(describing: self)
    }
}

You should make your cell class conform to NibLoadable if it comes with an accompanying nib file.

Cell setup ๐Ÿ”ง

// Any UITableViewCell automatically conforms to ReusableView
class SomeTableViewCell: UItableViewCell {}

// If a cell is loaded via a nib, it should conform to NibLoadable as well
class SomeOtherTableViewCell: UItableViewCell, NibLoadable {}

Which reduces this ๐Ÿ˜

class ViewController: UIViewController {

    func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(SomeTableViewCell.self, forCellReuseIdentifier: "SomeTableViewCell")
        tableView.register(UINib(nibName: "SomeOtherTableViewCell", bundle: nil), forCellReuseIdentifier: "SomeOtherTableViewCell")
    }

    // MARK: - UITableViewDataSource

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = dequeueReusableCell(withIdentifier: "SomeTableViewCell", for: indexPath) as! SomeTableViewCell
        cell.configure(thing: thing)

        return cell
    }

}

To just this ๐Ÿ™Œ

class ViewController: UIViewController {

    func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(class: SomeTableViewCell.self)
        tableView.register(class: SomeOtherTableViewCell.self)
    }

    // MARK: - UITableViewDataSource

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueCell(forIndexPath: indexPath) as SomeTableViewCell
        cell.configure(thing: thing)

        return cell
    }

}

Caveats

  • Class name, reuse identifier and nib name must match

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.