diff --git a/README.md b/README.md index c3adbf6..cd5aa86 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Build Status Swift 2.2 compatible Carthage compatible - CocoaPods compatible + CocoaPods compatible Platform iOS License: MIT

@@ -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 diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index ff6fd3f..81b8b5d 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -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 { diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index b3cd9d1..0655c10 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -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]() + private(set) public var editingActions: [UITableViewRowAction]? public var hashValue: Int { return ObjectIdentifier(self).hashValue @@ -70,9 +74,10 @@ public class TableRow]? = nil) { + public init(item: ItemType, actions: [TableRowAction]? = 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 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) -> Self { diff --git a/Sources/TableRowAction.swift b/Sources/TableRowAction.swift index 8407cb4..abae255 100644 --- a/Sources/TableRowAction.swift +++ b/Sources/TableRowAction.swift @@ -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) diff --git a/Sources/TableSection.swift b/Sources/TableSection.swift index e42b306..3e4df1d 100644 --- a/Sources/TableSection.swift +++ b/Sources/TableSection.swift @@ -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) } } \ No newline at end of file diff --git a/TableKit.podspec b/TableKit.podspec index ee47370..e688275 100644 --- a/TableKit.podspec +++ b/TableKit.podspec @@ -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.'