Skip to content

Commit 42d0bab

Browse files
author
Lorenzo
committed
added editing configuration and delegate
1 parent 82de6a1 commit 42d0bab

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎ALTableViewSwift/ALTableView/ALTableView/ALTableViewClasses/ALTableView/ALTableView.swift‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ public class ALTableView: NSObject {
1919

2020
internal var sectionElements: Array<ALSectionElement>
2121
public weak var delegate: ALTableViewProtocol?
22+
public weak var editingDelegate: ALTableViewRowEditingProtocol?
2223
public weak var viewController: UIViewController?
2324
public weak var tableView: UITableView?
25+
public var editingAllowed: Bool
26+
public var movingRowsAllowed: Bool
2427

2528

2629
//MARK: - Initializers
2730

28-
public init(sectionElements: Array<ALSectionElement>, viewController: UIViewController, tableView: UITableView) {
31+
public init(sectionElements: Array<ALSectionElement>,
32+
viewController: UIViewController,
33+
tableView: UITableView,
34+
editingAllowed: Bool = false,
35+
movingRowsAllowed: Bool = false) {
2936

3037
self.sectionElements = sectionElements
38+
self.editingAllowed = editingAllowed
39+
self.movingRowsAllowed = movingRowsAllowed
3140
super.init()
3241
self.viewController = viewController
3342
tableView.delegate = self

‎ALTableViewSwift/ALTableView/ALTableView/ALTableViewClasses/ALTableView/ALTableViewEditingManaging.swift‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88

99
import UIKit
1010

11+
public protocol ALTableViewRowEditingProtocol: class {
12+
func rowMoved(from sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
13+
func rowDeleted(indexPath: IndexPath)
14+
}
15+
1116
extension ALTableView {
1217

1318
public func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
1419

15-
return true
20+
return movingRowsAllowed
1621
}
1722

1823
public func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
@@ -23,19 +28,23 @@ extension ALTableView {
2328
if let originRowElement = originSection?.getRowElementAt(index: sourceIndexPath.row) {
2429
originSection?.deleteRowElements(numberOfRowElements: 1, at: .middle(sourceIndexPath.row))
2530
destinationSection?.insert(rowElements: [originRowElement], at: .middle(destinationIndexPath.row))
31+
editingDelegate?.rowMoved(from: sourceIndexPath, to: destinationIndexPath)
2632
}
2733
}
2834

2935
public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
3036

31-
return true
37+
return editingAllowed
3238
}
3339

3440
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
3541

3642
switch editingStyle {
3743
case .delete:
38-
self.remove(at: indexPath, animation: .automatic)
44+
if self.remove(at: indexPath, animation: .automatic) {
45+
editingDelegate?.rowDeleted(indexPath: indexPath)
46+
}
47+
3948
default:
4049
break
4150
}

0 commit comments

Comments
 (0)