GithubHelp home page GithubHelp logo

guddns / tablekit.m Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brunow/tablekit.m

0.0 2.0 0.0 343 KB

Create table view with blocks based syntax.

Home Page: https://twitter.com/brunowernimont

License: Apache License 2.0

Objective-C 99.72% Ruby 0.18% C++ 0.05% Shell 0.05%

tablekit.m's Introduction

TableKit.m

Create table view with blocks based syntax. This library is extracted from BaseKit.

Installation

Copy TableKit dir into your project.

Or with Cocoapods

pod 'TableKit.m', :git => "https://github.com/brunow/TableKit.m", :tag => "0.1.0"

Example code

self.tableModel = [TKListTableModel tableModelForTableView:self.tableView];
self.tableView.dataSource = self.tableModel;
self.tableView.delegate = self.tableModel;

[TKCellMapping mappingForObjectClass:[Item class] block:^(TKCellMapping *cellMapping) {
    [cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];

    [cellMapping onSelectRowWithBlock:^(UITableViewCell *cell, Item *item, NSIndexPath *indexPath) {
 	      // Do something
    }];

    [cellMapping mapObjectToCellClass:[UITableViewCell class]];
    [self.tableModel registerMapping:cellMapping];
}];

NSArray *items = [NSArray arrayWithObjects:
					[Item itemWithTitle:@"Simple mapping"],
					[Item itemWithTitle:@"Nib cell example"],
					nil];

[self.tableModel loadTableItems:items];

Models

TableKit provide many type of model that you can use.

TKTableModel

Basic model that doesn't handle data list. You will need to create a block that return an object for an indexPath.

[self.tableModel setObjectForRowAtIndexPathWithBlock:^id(NSIndexPath *indexPath) {
	return [self.items objectAtIndex:indexPath.row];
}];

TKListTableModel

Model that handle flat list of items without section support.

Loading items

- (void)loadTableItems:(NSArray *)items;

TKManagedTableModel

Model that conform to NSFetchedResultsControllerDelegate. All you need is to set a fetched result controller by using the methods below:

- (void)setFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController

Or by using block syntax:

- (void)setFetchedResultsControllerWithBlock:(TKManagedTableModelFetchedResultsControllerBlock)block

Load items

- (void)loadItems

As you can see it's very simple to create a fetch controller.

Create your model

After you've choosen which table model you want to use, you will need to initialize it.

self.tableModel = [TKListTableModel tableModelForTableView:self.tableView];

If you don't want to handle UITableViewDataSource and UITableViewDelegate, let TableKit handle it.

self.tableView.dataSource = self.tableModel;
self.tableView.delegate = self.tableModel;

Otherwise you will need to call table model method at hand.

Mapping

Simple mapping

[TKCellMapping mappingForObjectClass:[Item class] block:^(TKCellMapping *cellMapping) {
	[cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];
	[cellMapping mapObjectToCellClass:[UITableViewCell class]];
	[self.tableModel registerMapping:cellMapping];
}];

Dynamic mapping

[TKDynamicCellMapping mappingForObjectClass:[Item class] block:^(TKDynamicCellMapping *cellMapping) {
    [cellMapping mapKeyPath:@"title" toAttribute:@"textLabel.text"];
    [cellMapping mapObjectToCellClass:[MovieViewCell class] whenValueOfKeyPath:@"type" isEqualTo:@"movie"];
    [self.tableModel registerMapping:cellMapping];
}];

How does the mapping work ?

Take the example above. Item object contain a property named title and MovieViewCell contain a UILabel called textLabel that herself has a property text to changed the label text.

Migrating from BaseKit CellMapping

Classes are now prefixed with TK instead of BK so just rename classes. The second change is that now you've different type of model described above.

ARC

TableKit is ARC only and require IOS 5.

Changelog

  • Master branch

Todo

Create sectioned model.

Contact

Bruno Wernimont

tablekit.m's People

Contributors

brunow avatar

Watchers

James Cloos avatar HyeongUn Kim 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.