diff --git a/README.md b/README.md index fd08b41..e047841 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,17 @@ ![Tablet](https://raw.githubusercontent.com/maxsokolov/tablet/assets/tablet.png) -Tablet is a super lightweight yet powerful generic library that handles a complexity of UITableView's datasource and delegate 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 statement when you deal with bunch of different cells in different sections. +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. + +That's almost all you need in your controller to build a bunch of cells in a section: +```swift +TableConfigurableRowBuilder(items: ["1", "2", "3", "4", "5"], estimatedRowHeight: 42) +``` +Tablet respects cells reusability feature and it's type-safe. See the Usage section to learn more. ## Requirements - iOS 8.0+ -- Xcode 7.0+ +- Xcode 7.1+ ## Installation @@ -30,14 +36,16 @@ $ pod install ### Very basic +You may want to setup a very basic table view, without any custom cells. In that case simply use the `TableRowBuilder`. + ```swift import Tablet let rowBuilder = TableRowBuilder(items: [user1, user2, user3], id: "reusable_id") .action(.configure) { data in - data.cell.textLabel?.text = data.item.title - data.cell.detailTextLabel?.text = data.item.isActive ? "Active" : "Inactive" + data.cell?.textLabel?.text = data.item.username + data.cell?.detailTextLabel?.text = data.item.isActive ? "Active" : "Inactive" } let sectionBuilder = TableSectionBuilder(headerTitle: "Users", rowBuilders: [rowBuilder]) @@ -46,6 +54,40 @@ let director = TableDirector(tableView: tableView) director.appendSections(sectionBuilder) ``` +### 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 `TableConfigurableRowBuilder`. Your cell should respect the `ConfigurableCell` protocol as you may see in example below: + +```swift +import Tablet + +class MyTableViewCell : UITableViewCell, ConfigurableCell { + + typealias Item = User + + static func reusableIdentifier() -> String { + return "reusable_id" + } + + func configureWithItem(item: Item) { // item is user here + + textLabel?.text = item.username + detailTextLabel?.text = item.isActive ? "Active" : "Inactive" + } +} +``` +Once you've implemented the protocol, simply use the `TableConfigurableRowBuilder` to build cells: + +```swift +import Tablet + +let rowBuilder = TableConfigurableRowBuilder() +rowBuilder.appendItems(users) + +tableDirector.appendSection(TableSectionBuilder(rowBuilders: [rowBuilder])) +``` + ### Cell actions Tablet provides a chaining approach to handle actions from your cells: @@ -65,40 +107,6 @@ let rowBuilder = TableRowBuilder(items: [user1, user2, us } ``` -### 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. -To provide this behaviour simply follow: - -```swift -import Tablet - -class UserTableViewCell : UITableViewCell, ConfigurableCell { - - typealias Item = User - - static func reusableIdentifier() -> String { - return "reusable_id" - } - - func configureWithItem(item: Item) { // item is user here - - textLabel?.text = item.title - detailTextLabel?.text = item.isActive ? "Active" : "Inactive" - } -} -``` -Once you follow the protocol, simply use TableConfigurableRowBuilder to build cells: - -```swift -import Tablet - -let rowBuilder = TableConfigurableRowBuilder() -rowBuilder.appendItems(users) - -tableDirector.appendSection(TableSectionBuilder(rowBuilders: [rowBuilder])) -``` - ### Custom cell actions ```swift import Tablet diff --git a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate index 61803cb..7cb6250 100644 Binary files a/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate and b/TabletDemo/TabletDemo.xcodeproj/project.xcworkspace/xcuserdata/maxsokolov.xcuserdatad/UserInterfaceState.xcuserstate differ