Code Monkey home page Code Monkey logo

tablet.swift's Introduction

Alamofire: Elegant Networking in Swift

#Tablet.swift

Build Status Swift 2.2 compatible Platform iOS CocoaPods compatible License: MIT

Tablet is a super lightweight yet powerful generic library that handles a complexity of UITableView's datasource and delegate methods in a Swift environment. Tablet's goal is to provide an easiest way to create complex table views. With Tablet you don't have to write a messy code of switch or if statements when you deal with bunch of different cells in different sections.

Features

  • Type-safe cells based on generics
  • The easiest way to map your models or view models to cells
  • Correctly handles autolayout cells with multiline labels
  • Chainable cell actions (select/deselect etc.)
  • Support cells created from code, xib, or storyboard
  • Automatic xib/classes registration
  • No need to subclass
  • Extensibility
  • Tests

That's almost all you need to build a bunch of cells in a section:

let builder = TableRowBuilder<String, MyTableViewCell>(items: ["1", "2", "3", "4", "5"])

Tablet relies on self-sizing table view cells, respects cells reusability feature and also built with performace in mind. You don't have to worry about anything, just create your cells, setup autolayout constraints and be happy. See the Usage section to learn more.

Requirements

  • iOS 8.0+
  • Xcode 7.0+
  • Swift 2.2

Installation

CocoaPods

To integrate Tablet into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Tablet'

Then, run the following command:

$ pod install

Usage

Type-safe configurable cells

Let's say you want to put your cell configuration logic into cell itself. Say you want to pass your view model (or even model) to your cell. You could easily do this using the TableRowBuilder. Your cell should conforms to ConfigurableCell protocol as you may see in example below:

import Tablet

class MyTableViewCell : UITableViewCell, ConfigurableCell {

	typealias T = User

	// this method is not required to be implemented if your cell's id equals to class name
	static func reusableIdentifier() -> String {
		return "reusable_id"
	}

	static func estimatedHeight() -> Float {
        return 255
    }

	func configure(item: T) { // item is user here

		textLabel?.text = item.username
		detailTextLabel?.text = item.isActive ? "Active" : "Inactive"
	}
}

Once you've implemented the protocol, simply use the TableRowBuilder to build cells:

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>()
rowBuilder += users

director = TableDirector(tableView: tableView)
tableDirector += TableSectionBuilder(rows: [rowBuilder])

Very basic table view

You may want to setup a very basic table view, without any custom cells. In that case simply use the TableBaseRowBuilder.

import Tablet

let rowBuilder = TableBaseRowBuilder<User, UITableViewCell>(items: [user1, user2, user3], id: "reusable_id")
	.action(.configure) { (data) in

		data.cell?.textLabel?.text = data.item.username
		data.cell?.detailTextLabel?.text = data.item.isActive ? "Active" : "Inactive"
	}

let sectionBuilder = TableSectionBuilder(headerTitle: "Users", footerTitle: nil, rows: [rowBuilder])

director = TableDirector(tableView: tableView)
director += sectionBuilder

Cell actions

Tablet provides a chaining approach to handle actions from your cells:

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: [user1, user2, user3], id: "reusable_id")
	.action(.configure) { (data) in

	}
	.action(.click) { (data) in

	}
	.valueAction(.shouldHighlight) { (data) in

		return false
	}

Custom cell actions

import Tablet

struct MyCellActions {
	static let ButtonClicked = "ButtonClicked"
}

class MyTableViewCell : UITableViewCell {

	@IBAction func buttonClicked(sender: UIButton) {

		Action(key: MyCellActions.ButtonClicked, sender: self, userInfo: nil).invoke()
	}
}

And receive this actions with your row builder:

import Tablet

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
	.action(.click) { (data) in
		
	}
	.action(.willDisplay) { (data) in
		
	}
	.action(MyCellActions.ButtonClicked) { (data) in
		
	}

Extensibility

If you find that Tablet is not provide an action you need, for example you need UITableViewDelegate's didEndDisplayingCell method and it's not out of the box, simply provide an extension for TableDirector:

import Tablet

struct MyTableActions {
	static let DidEndDisplayingCell = "DidEndDisplayingCell"
}

extension TableDirector {

	public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

		invoke(action: .custom(MyTableActions.DidEndDisplayingCell), cell: cell, indexPath: indexPath)
	}
}

Catch your action with row builder:

let rowBuilder = TableRowBuilder<User, MyTableViewCell>(items: users)
	.action(MyTableActions.DidEndDisplayingCell) { (data) -> Void in

	}

You could also invoke an action that returns a value.

License

Tablet is available under the MIT license. See LICENSE for details.

tablet.swift's People

Contributors

maxsokolov avatar

Watchers

James Cloos avatar Artem Abramov 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.