Merge pull request #36 from maxsokolov/develop

Support editing actions
This commit is contained in:
Max Sokolov 2016-08-22 19:11:07 +04:00 committed by GitHub
commit 39eda5a8f3
6 changed files with 49 additions and 17 deletions

View File

@ -4,7 +4,7 @@
<a href="https://travis-ci.org/maxsokolov/TableKit"><img src="https://api.travis-ci.org/maxsokolov/TableKit.svg" alt="Build Status" /></a>
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift_2.2-compatible-4BC51D.svg?style=flat" alt="Swift 2.2 compatible" /></a>
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-1.0.0-blue.svg" alt="CocoaPods compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-1.1.0-blue.svg" alt="CocoaPods compatible" /></a>
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
<a href="https://raw.githubusercontent.com/maxsokolov/tablekit/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
</p>
@ -184,7 +184,7 @@ tableDirector += rows
Done, your table is ready.
## Automatic cell registration
TableKit can register your cells in table view automatically. In case if your reusable cell id mathces cell's xib name:
TableKit can register your cells in a table view automatically. In case if your reusable cell id mathces cell's xib name:
```ruby
MyTableViewCell.swift

View File

@ -70,7 +70,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: Public
public func invoke(action action: TableRowActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> Any? {
return sections[indexPath.section].items[indexPath.row].invoke(action, cell: cell, path: indexPath)
return sections[indexPath.section].rows[indexPath.row].invoke(action, cell: cell, path: indexPath)
}
public override func respondsToSelector(selector: Selector) -> Bool {
@ -84,7 +84,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: - Internal -
func hasAction(action: TableRowActionType, atIndexPath indexPath: NSIndexPath) -> Bool {
return sections[indexPath.section].items[indexPath.row].hasAction(action)
return sections[indexPath.section].rows[indexPath.row].hasAction(action)
}
func didReceiveAction(notification: NSNotification) {
@ -97,13 +97,13 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let row = sections[indexPath.section].items[indexPath.row]
let row = sections[indexPath.section].rows[indexPath.row]
return row.estimatedHeight ?? heightStrategy?.estimatedHeight(row, path: indexPath) ?? UITableViewAutomaticDimension
}
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let row = sections[indexPath.section].items[indexPath.row]
let row = sections[indexPath.section].rows[indexPath.row]
let rowHeight = invoke(action: .height, cell: nil, indexPath: indexPath) as? CGFloat
return rowHeight ?? row.defaultHeight ?? heightStrategy?.height(row, path: indexPath) ?? UITableViewAutomaticDimension
@ -121,7 +121,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = sections[indexPath.section].items[indexPath.row]
let row = sections[indexPath.section].rows[indexPath.row]
cellManager?.register(cellType: row.cellType, forCellReuseIdentifier: row.reuseIdentifier)
@ -203,6 +203,23 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
return indexPath
}
// MARK: - Row editing -
public func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath)
}
public func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
return sections[indexPath.section].rows[indexPath.row].editingActions
}
public func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
invoke(action: .clickDelete, cell: tableView.cellForRowAtIndexPath(indexPath), indexPath: indexPath)
}
}
// MARK: - Sections manipulation -
public func append(section section: TableSection) -> Self {

View File

@ -27,6 +27,9 @@ public protocol RowConfigurable {
public protocol RowActionable {
var editingActions: [UITableViewRowAction]? { get }
func isEditingAllowed(forIndexPath indexPath: NSIndexPath) -> Bool
func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any?
func hasAction(action: TableRowActionType) -> Bool
}
@ -49,6 +52,7 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
public let item: ItemType
private lazy var actions = [String: TableRowAction<ItemType, CellType>]()
private(set) public var editingActions: [UITableViewRowAction]?
public var hashValue: Int {
return ObjectIdentifier(self).hashValue
@ -70,9 +74,10 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
return CellType.self
}
public init(item: ItemType, actions: [TableRowAction<ItemType, CellType>]? = nil) {
public init(item: ItemType, actions: [TableRowAction<ItemType, CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
self.item = item
self.editingActions = editingActions
actions?.forEach { self.actions[$0.type.key] = $0 }
}
@ -92,6 +97,14 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
return actions[action.key] != nil
}
public func isEditingAllowed(forIndexPath indexPath: NSIndexPath) -> Bool {
if actions[TableRowActionType.canEdit.key] != nil {
return invoke(.canEdit, cell: nil, path: indexPath) as? Bool ?? false
}
return editingActions?.isEmpty == false || actions[TableRowActionType.clickDelete.key] != nil
}
// MARK: - actions -
public func action(action: TableRowAction<ItemType, CellType>) -> Self {

View File

@ -23,12 +23,14 @@ import UIKit
public enum TableRowActionType {
case click
case clickDelete
case select
case deselect
case willSelect
case willDisplay
case shouldHighlight
case height
case canEdit
case configure
case custom(String)

View File

@ -24,7 +24,7 @@ public class TableSection {
weak var tableDirector: TableDirector?
public private(set) var items = [Row]()
public private(set) var rows = [Row]()
public var headerTitle: String?
public var footerTitle: String?
@ -36,17 +36,17 @@ public class TableSection {
public var footerHeight: CGFloat? = nil
public var numberOfRows: Int {
return items.count
return rows.count
}
public var isEmpty: Bool {
return items.isEmpty
return rows.isEmpty
}
public init(rows: [Row]? = nil) {
if let initialRows = rows {
items.appendContentsOf(initialRows)
self.rows.appendContentsOf(initialRows)
}
}
@ -67,7 +67,7 @@ public class TableSection {
// MARK: - Public -
public func clear() {
items.removeAll()
rows.removeAll()
}
public func append(row row: Row) {
@ -75,14 +75,14 @@ public class TableSection {
}
public func append(rows rows: [Row]) {
items.appendContentsOf(rows)
self.rows.appendContentsOf(rows)
}
public func insert(row row: Row, atIndex index: Int) {
items.insert(row, atIndex: index)
rows.insert(row, atIndex: index)
}
public func delete(index index: Int) {
items.removeAtIndex(index)
rows.removeAtIndex(index)
}
}

View File

@ -2,7 +2,7 @@ Pod::Spec.new do |s|
s.name = 'TableKit'
s.module_name = 'TableKit'
s.version = '1.0.0'
s.version = '1.1.0'
s.homepage = 'https://github.com/maxsokolov/TableKit'
s.summary = 'Type-safe declarative table views. Swift 2.2 is required.'