From a7530eb3c779e144444cc5cea189971a952003a6 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 22 Aug 2016 17:31:54 +0300 Subject: [PATCH 1/5] rename items to rows in TableSection --- Sources/TableDirector.swift | 10 +++++----- Sources/TableSection.swift | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index ff6fd3f..bfdb60e 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) 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 From 5e5643c342992475e7c70df17e88c49014ac71df Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 22 Aug 2016 17:33:28 +0300 Subject: [PATCH 2/5] support editing actions in TableRow --- Sources/TableRow.swift | 15 ++++++++++++++- Sources/TableRowAction.swift | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) 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) From 60003d75573dc2f65886b40f8b95681202c7ae0b Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 22 Aug 2016 17:36:08 +0300 Subject: [PATCH 3/5] support editing actions in TableDirector --- Sources/TableDirector.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index bfdb60e..81b8b5d 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -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 { From 99fc12e7238de26ef930979a88a2b6cc93a37df2 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 22 Aug 2016 17:39:30 +0300 Subject: [PATCH 4/5] bump readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 85cf81ecd6f845413dcbe06f10f109df7102e5da Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Mon, 22 Aug 2016 17:40:05 +0300 Subject: [PATCH 5/5] bump podspec --- TableKit.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.'