diff --git a/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutItem.swift b/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutItem.swift index f6bebdc..87d0dbf 100644 --- a/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutItem.swift +++ b/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutItem.swift @@ -23,14 +23,16 @@ enum MoreAboutItemType { } } -class MoreAboutItem: Item, Selectable { - +class MoreAboutItem: Item, Selectable, Editable { + var type: MoreAboutItemType var title: String? var drawer: CellDrawer.Type = MoreAboutDrawer.self var onSelection: (Selectable) -> () = { _ in } - + + var actions: [UITableViewRowAction]? + init(type: MoreAboutItemType) { self.type = type diff --git a/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutSection.swift b/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutSection.swift index 9f882ea..cb0a6dd 100644 --- a/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutSection.swift +++ b/Examples/Viper/TableViewKit+VIPER/AboutModule/MoreAboutSection.swift @@ -13,6 +13,13 @@ class MoreAboutSection: Section { self.presenter = presenter self.manager = manager + let moreAction = UITableViewRowAction(style: .normal, title: "More", handler: { action, indexPath in + print("MoreAction executed") + }) + let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete", handler: { action, indexPath in + self.items.remove(at: indexPath.row) + }) + let types: [MoreAboutItemType] = [.faq, .contact, .terms, .feedback, .share, .rate] for type in types { let moreAboutItem = MoreAboutItem(type: type) @@ -36,6 +43,7 @@ class MoreAboutSection: Section { guard let manager = self.manager else { return } item.deselect(in: manager, animated: true) } + moreAboutItem.actions = [deleteAction, moreAction] items.append(moreAboutItem) } } diff --git a/TableViewKit.podspec b/TableViewKit.podspec index 101da82..3417f31 100644 --- a/TableViewKit.podspec +++ b/TableViewKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "TableViewKit" - s.version = "0.9.2" + s.version = "0.9.3" s.summary = "Empowering UITableView with painless multi-type cell support and build-in automatic state transition animations" s.homepage = "http://github.com/odigeoteam/TableViewKit/" s.license = "MIT" diff --git a/TableViewKit.xcodeproj/project.pbxproj b/TableViewKit.xcodeproj/project.pbxproj index e99a751..49be4e7 100644 --- a/TableViewKit.xcodeproj/project.pbxproj +++ b/TableViewKit.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 2564E61A1D88419B00A9DC3E /* TestRegisterNibCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2564E6191D88419B00A9DC3E /* TestRegisterNibCell.xib */; }; 2564E61C1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2564E61B1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib */; }; 25837C781D87F819001EF4B8 /* ItemCompatible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25837C771D87F819001EF4B8 /* ItemCompatible.swift */; }; + 25B0B7541DC74F6C00591467 /* Editable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25B0B7531DC74F6C00591467 /* Editable.swift */; }; CC5CF11C1D839E71004DECB3 /* ArrayDiff.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC5CF11B1D839E71004DECB3 /* ArrayDiff.swift */; }; CC97D76D1D741DC4009CDF9D /* Selectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC97D76C1D741DC4009CDF9D /* Selectable.swift */; }; CC9D50E11D8496180010FCA3 /* TableViewDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC9D50E01D8496180010FCA3 /* TableViewDelegateTests.swift */; }; @@ -47,6 +48,7 @@ 2564E6191D88419B00A9DC3E /* TestRegisterNibCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestRegisterNibCell.xib; sourceTree = ""; }; 2564E61B1D88450F00A9DC3E /* TestRegisterHeaderFooterView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TestRegisterHeaderFooterView.xib; sourceTree = ""; }; 25837C771D87F819001EF4B8 /* ItemCompatible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ItemCompatible.swift; path = Protocols/ItemCompatible.swift; sourceTree = ""; }; + 25B0B7531DC74F6C00591467 /* Editable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Editable.swift; path = Protocols/Editable.swift; sourceTree = ""; }; 4BCAD51C1D89A704002F3420 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = "External/Nimble/build/Debug-iphoneos/Nimble.framework"; sourceTree = ""; }; CC5CF11B1D839E71004DECB3 /* ArrayDiff.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayDiff.swift; sourceTree = ""; }; CC97D76C1D741DC4009CDF9D /* Selectable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Selectable.swift; path = Protocols/Selectable.swift; sourceTree = ""; }; @@ -111,6 +113,7 @@ CCEF387F1D8D5A7A00F5893F /* HeaderFooterDrawer.swift */, CCBE458C1D72F79C00414A64 /* Item.swift */, CC97D76C1D741DC4009CDF9D /* Selectable.swift */, + 25B0B7531DC74F6C00591467 /* Editable.swift */, 25837C771D87F819001EF4B8 /* ItemCompatible.swift */, CCBE458A1D72F79C00414A64 /* CellDrawer.swift */, ); @@ -292,6 +295,7 @@ CC97D76D1D741DC4009CDF9D /* Selectable.swift in Sources */, CCCAC1281D7DA2D00001FC1D /* Height.swift in Sources */, CC5CF11C1D839E71004DECB3 /* ArrayDiff.swift in Sources */, + 25B0B7541DC74F6C00591467 /* Editable.swift in Sources */, 25837C781D87F819001EF4B8 /* ItemCompatible.swift in Sources */, CCEF38801D8D5A7A00F5893F /* HeaderFooterDrawer.swift in Sources */, CCEF387E1D8D59C000F5893F /* NibClassType.swift in Sources */, diff --git a/TableViewKit/Protocols/Editable.swift b/TableViewKit/Protocols/Editable.swift new file mode 100644 index 0000000..a3b1790 --- /dev/null +++ b/TableViewKit/Protocols/Editable.swift @@ -0,0 +1,8 @@ +import Foundation + +/// A type that represent an item that can be edited +public protocol Editable: Item { + + /// The associated actions + var actions: [UITableViewRowAction]? { get set } +} diff --git a/TableViewKit/Protocols/Stateful.swift b/TableViewKit/Protocols/Stateful.swift index bdd7cd3..2627159 100644 --- a/TableViewKit/Protocols/Stateful.swift +++ b/TableViewKit/Protocols/Stateful.swift @@ -1,11 +1,3 @@ -// -// Stateful.swift -// TableViewKit -// -// Created by Alfredo Delli Bovi on 23/09/2016. -// Copyright © 2016 odigeo. All rights reserved. -// - import Foundation /// A section that supports states. diff --git a/TableViewKit/TableViewManager.swift b/TableViewKit/TableViewManager.swift index 0cbf9e3..38e113f 100644 --- a/TableViewKit/TableViewManager.swift +++ b/TableViewKit/TableViewManager.swift @@ -160,7 +160,10 @@ extension TableViewManager: UITableViewDataSource { return title(for: {$0.footer}, inSection: section) } - + /// Implementation of UITableViewDataSource + public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { + // Intentionally blank. Required to use UITableViewRowActions + } } extension TableViewManager: UITableViewDelegate { @@ -211,4 +214,9 @@ extension TableViewManager: UITableViewDelegate { return view(for: {$0.footer}, inSection: section) } + /// Implementation of UITableViewDelegate + public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { + guard let item = item(at: indexPath) as? Editable else { return nil } + return item.actions + } } diff --git a/TableViewKitTests/TableViewDelegateTests.swift b/TableViewKitTests/TableViewDelegateTests.swift index 4157703..707c85b 100644 --- a/TableViewKitTests/TableViewDelegateTests.swift +++ b/TableViewKitTests/TableViewDelegateTests.swift @@ -86,6 +86,10 @@ class SelectableItem: Selectable, Item { } } +class EditableItem: SelectableItem, Editable { + public var actions: [UITableViewRowAction]? +} + class TableViewDelegateTests: XCTestCase { @@ -206,4 +210,22 @@ class TableViewDelegateTests: XCTestCase { expect(check).to(equal(2)) } + func testEditableRows() { + let section = tableViewManager.sections.first! + let deleteAction = UITableViewRowAction(style: .normal, title: "Delete", handler: { action, indexPath in + print("DeleteAction") + }) + let editableItem = EditableItem { _ in } + editableItem.actions = [deleteAction] + section.items.append(editableItem) + + let sectionIndex = section.index(in: tableViewManager)! + let rows = tableViewManager.tableView(tableViewManager.tableView, numberOfRowsInSection: sectionIndex) + XCTAssert(rows == 2) + + let indexPath = editableItem.indexPath(in: tableViewManager)! + let actions = tableViewManager.tableView(tableViewManager.tableView, editActionsForRowAt: indexPath) + XCTAssertNotNil(actions) + XCTAssert(actions!.count == 1) + } }