Compare commits

...

4 Commits

Author SHA1 Message Date
Max Sokolov 8f895bdd31 bump podspec 2016-05-24 11:55:51 +03:00
Max Sokolov 79aef4dd39 fix clickDelete event 2016-05-24 11:55:09 +03:00
Max Sokolov b1f3b110b1 support can edit 2016-05-23 21:43:14 +03:00
Max Sokolov b5f7bd25e6 compatibility fix 2016-05-13 17:28:27 +03:00
6 changed files with 118 additions and 92 deletions

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'Tablet' s.name = 'Tablet'
s.version = '0.2.6' s.version = '0.2.8'
s.homepage = 'https://github.com/maxsokolov/tablet' s.homepage = 'https://github.com/maxsokolov/tablet'
s.summary = 'Powerful type-safe tool for UITableView. Swift 2.0 is required.' s.summary = 'Powerful type-safe tool for UITableView. Swift 2.0 is required.'
@ -12,5 +12,5 @@ Pod::Spec.new do |s|
s.source_files = 'Tablet/*.swift' s.source_files = 'Tablet/*.swift'
s.module_name = 'Tablet' s.module_name = 'Tablet'
s.source = { :git => 'https://github.com/maxsokolov/tablet.git', :tag => s.version } s.source = { :git => 'https://github.com/maxsokolov/Tablet.swift.git', :tag => s.version }
end end

View File

@ -22,7 +22,7 @@ import UIKit
import Foundation import Foundation
/** /**
Responsible for table view's datasource and delegate. Responsible for table view's datasource and delegate.
*/ */
public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
@ -37,7 +37,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
self.tableView.delegate = self self.tableView.delegate = self
self.tableView.dataSource = self self.tableView.dataSource = self
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveAction:", name: kActionPerformedNotificationKey, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didReceiveAction), name: kActionPerformedNotificationKey, object: nil)
} }
deinit { deinit {
@ -48,13 +48,13 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: Private methods // MARK: Private methods
/** /**
Find a row builder that responsible for building a row from cell with given item type. Find a row builder that responsible for building a row from cell with given item type.
- Parameters: - Parameters:
- indexPath: path of cell to dequeue - indexPath: path of cell to dequeue
- Returns: A touple - (builder, builderItemIndex) - Returns: A touple - (builder, builderItemIndex)
*/ */
private func builderAtIndexPath(indexPath: NSIndexPath) -> (RowBuilder, Int) { private func builderAtIndexPath(indexPath: NSIndexPath) -> (RowBuilder, Int) {
return sections[indexPath.section].builderAtIndex(indexPath.row)! return sections[indexPath.section].builderAtIndex(indexPath.row)!
@ -217,6 +217,20 @@ public extension TableDirector {
} }
} }
public extension TableDirector {
public func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return invokeAction(.canEdit, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath) as? Bool ?? false
}
public func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
invokeAction(.clickDelete, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath)
}
}
}
public func +=(left: TableDirector, right: RowBuilder) { public func +=(left: TableDirector, right: RowBuilder) {
left.appendSection(TableSectionBuilder(rowBuilders: [right])) left.appendSection(TableSectionBuilder(rowBuilders: [right]))

View File

@ -41,12 +41,12 @@ internal enum ActionHandler<I, C> {
} }
/** /**
Responsible for building cells of given type and passing items to them. Responsible for building cells of given type and passing items to them.
*/ */
public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder { public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
private var actions = Dictionary<String, ActionHandler<I, C>>() private var actions = Dictionary<String, ActionHandler<I, C>>()
private var items = [I]() public var items = [I]()
public var reusableIdentifier: String public var reusableIdentifier: String
public var estimatedRowHeight: CGFloat public var estimatedRowHeight: CGFloat
@ -125,8 +125,8 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
} }
/** /**
Responsible for building configurable cells of given type and passing items to them. Responsible for building configurable cells of given type and passing items to them.
*/ */
public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item == I, C: UITableViewCell> : TableRowBuilder<I, C> { public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item == I, C: UITableViewCell> : TableRowBuilder<I, C> {
public init(item: I, estimatedRowHeight: CGFloat = 48) { public init(item: I, estimatedRowHeight: CGFloat = 48) {
@ -152,6 +152,13 @@ public extension TableRowBuilder {
// MARK: Items manipulation // MARK: Items manipulation
public func removeItemAtIndex(index: Int) {
if index < items.count {
items.removeAtIndex(index)
}
}
public func appendItems(items: [I]) { public func appendItems(items: [I]) {
self.items.appendContentsOf(items) self.items.appendContentsOf(items)

View File

@ -67,13 +67,14 @@ public class TableSectionBuilder {
internal extension TableSectionBuilder { internal extension TableSectionBuilder {
internal func builderAtIndex(var index: Int) -> (RowBuilder, Int)? { internal func builderAtIndex(index: Int) -> (RowBuilder, Int)? {
var builderIndex = index
for builder in builders { for builder in builders {
if index < builder.numberOfRows { if builderIndex < builder.numberOfRows {
return (builder, index) return (builder, builderIndex)
} }
index -= builder.numberOfRows builderIndex -= builder.numberOfRows
} }
return nil return nil

View File

@ -24,8 +24,8 @@ import Foundation
internal let kActionPerformedNotificationKey = "_action" internal let kActionPerformedNotificationKey = "_action"
/** /**
The actions that Tablet provides. The actions that Tablet provides.
*/ */
public enum ActionType { public enum ActionType {
case click case click
@ -36,6 +36,8 @@ public enum ActionType {
case willDisplay case willDisplay
case shouldHighlight case shouldHighlight
case height case height
case canEdit
case clickDelete
case custom(String) case custom(String)
var key: String { var key: String {
@ -66,9 +68,9 @@ public class ActionData<I, C> {
} }
/** /**
A custom action that you can trigger from your cell. A custom action that you can trigger from your cell.
You can eacily catch actions using a chaining manner with your row builder. You can eacily catch actions using a chaining manner with your row builder.
*/ */
public class Action { public class Action {
/// The cell that triggers an action. /// The cell that triggers an action.
@ -94,12 +96,12 @@ public class Action {
} }
/** /**
If you want to delegate your cell configuration logic to cell itself (with your view model or even model) than If you want to delegate your cell configuration logic to cell itself (with your view model or even model) than
just provide an implementation of this protocol for your cell. Enjoy safe-typisation. just provide an implementation of this protocol for your cell. Enjoy safe-typisation.
*/ */
public protocol ConfigurableCell { public protocol ConfigurableCell {
typealias Item associatedtype Item
static func reusableIdentifier() -> String static func reusableIdentifier() -> String
func configureWithItem(item: Item) func configureWithItem(item: Item)
@ -114,15 +116,17 @@ public extension ConfigurableCell where Self: UITableViewCell {
} }
/** /**
A protocol that every row builder should follow. A protocol that every row builder should follow.
A certain section can only works with row builders that respect this protocol. A certain section can only works with row builders that respect this protocol.
*/ */
public protocol RowBuilder { public protocol RowBuilder {
var numberOfRows: Int { get } var numberOfRows: Int { get }
var reusableIdentifier: String { get } var reusableIdentifier: String { get }
var estimatedRowHeight: CGFloat { get } var estimatedRowHeight: CGFloat { get }
func removeItemAtIndex(index: Int)
func registerCell(inTableView tableView: UITableView) func registerCell(inTableView tableView: UITableView)
func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject?
} }