Skip to content

Commit 91dfdec

Browse files
committed
fixed editActions for iOS 11+
1 parent 02ec294 commit 91dfdec

4 files changed

Lines changed: 81 additions & 20 deletions

File tree

DataSource/CellDescriptor.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,8 @@ public class CellDescriptor<Item, Cell: UITableViewCell>: CellDescriptorType {
389389

390390
// MARK: swipeActions
391391

392-
fileprivate var leftSwipeActionClosure: ((RowType, IndexPath) -> Any?)?
393-
394-
fileprivate var rightSwipeActionClosure: ((RowType, IndexPath) -> Any?)?
392+
private var _leadingSwipeActionsClosure: ((RowType, IndexPath) -> Any?)?
393+
private var _trailingSwipeActionsClosure: ((RowType, IndexPath) -> Any?)?
395394

396395
// MARK: editActions
397396

@@ -585,28 +584,43 @@ extension CellDescriptor: CellDescriptorTypeiOS11 {
585584

586585
public var leadingSwipeActionsClosure: ((RowType, IndexPath) -> UISwipeActionsConfiguration?)? {
587586
get {
587+
if _leadingSwipeActionsClosure == nil {
588+
return nil
589+
}
590+
588591
return { [weak self] (rowType, indexPath) in
589-
return self?.leftSwipeActionClosure?(rowType, indexPath) as? UISwipeActionsConfiguration
592+
return self?._leadingSwipeActionsClosure?(rowType, indexPath) as? UISwipeActionsConfiguration
590593
}
591594
}
592595
}
593596

594597
public var trailingSwipeActionsClosure: ((RowType, IndexPath) -> UISwipeActionsConfiguration?)? {
595598
get {
599+
if _trailingSwipeActionsClosure == nil {
600+
return nil
601+
}
602+
596603
return { [weak self] (rowType, indexPath) in
597-
return self?.rightSwipeActionClosure?(rowType, indexPath) as? UISwipeActionsConfiguration
604+
return self?._trailingSwipeActionsClosure?(rowType, indexPath) as? UISwipeActionsConfiguration
598605
}
599606
}
600607
}
601608

602609
public func leadingSwipeAction(_ closure: @escaping ((RowType, IndexPath) -> UISwipeActionsConfiguration?)) -> CellDescriptor {
603-
leftSwipeActionClosure = closure
610+
_leadingSwipeActionsClosure = closure
604611
return self
605612
}
606613

607614
public func trailingSwipeAction(_ closure: @escaping ((RowType, IndexPath) -> UISwipeActionsConfiguration?)) -> CellDescriptor {
608-
rightSwipeActionClosure = closure
615+
_trailingSwipeActionsClosure = closure
609616
return self
610617
}
611618

619+
public var hasLeadingSwipeAction: Bool {
620+
return _leadingSwipeActionsClosure != nil
621+
}
622+
623+
public var hasTrailingSwipeAction: Bool {
624+
return _trailingSwipeActionsClosure != nil
625+
}
612626
}

DataSource/DataSource.swift

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public class DataSource: NSObject {
5353
public var willBeginEditing: ((RowType, IndexPath) -> Void)? = nil
5454
public var didEndEditing: ((IndexPath?) -> Void)? = nil
5555

56-
// MARK: iOS 11
57-
//See extension
58-
fileprivate var leftSwipeActions: ((RowType, IndexPath) -> Any?)? = nil
59-
fileprivate var rightSwipeActions: ((RowType, IndexPath) -> Any?)? = nil
60-
6156
public var sectionHeader: ((SectionType, Int) -> HeaderFooter)? = nil
6257
public var sectionFooter: ((SectionType, Int) -> HeaderFooter)? = nil
6358

@@ -77,6 +72,10 @@ public class DataSource: NSObject {
7772
public var performAction: ((RowType, Selector, Any?, IndexPath) -> Void)? = nil
7873
public var canFocus: ((RowType, IndexPath) -> Bool)? = nil
7974

75+
// MARK: Swipe Actions (iOS 11+ only)
76+
private var _leadingSwipeActions: ((RowType, IndexPath) -> Any?)? = nil
77+
private var _trailingSwipeActions: ((RowType, IndexPath) -> Any?)? = nil
78+
8079
// MARK: UITableViewDataSourcePrefetching
8180

8281
public var prefetchRows: (([IndexPath]) -> Void)? = nil
@@ -96,10 +95,48 @@ public class DataSource: NSObject {
9695

9796
public override func responds(to aSelector: Selector!) -> Bool {
9897
if super.responds(to: aSelector) {
98+
if #available(iOS 11.0, *) {
99+
switch aSelector {
100+
case #selector(UITableViewDelegate.tableView(_:leadingSwipeActionsConfigurationForRowAt:)):
101+
return isLeadingSwipeActionsImplemented ? true : fallbackDelegateResponds(to: aSelector)
102+
case #selector(UITableViewDelegate.tableView(_:trailingSwipeActionsConfigurationForRowAt:)):
103+
return isTrailingSwipeActionsImplemented ? true : fallbackDelegateResponds(to: aSelector)
104+
default:
105+
return true
106+
}
107+
} else {
108+
return true
109+
}
110+
}
111+
112+
return fallbackDelegateResponds(to: aSelector)
113+
}
114+
115+
private func fallbackDelegateResponds(to aSelector: Selector!) -> Bool {
116+
let result = fallbackDelegate?.responds(to: aSelector) ?? false
117+
118+
print(aSelector)
119+
print(result)
120+
121+
return result
122+
}
123+
124+
@available(iOS 11.0, *)
125+
private var isLeadingSwipeActionsImplemented: Bool {
126+
if _leadingSwipeActions != nil {
99127
return true
100-
} else {
101-
return fallbackDelegate?.responds(to: aSelector) ?? false
102128
}
129+
130+
return cellDescriptors.values.contains(where: { ($0 as? CellDescriptorTypeiOS11)?.leadingSwipeActionsClosure != nil })
131+
}
132+
133+
@available(iOS 11.0, *)
134+
private var isTrailingSwipeActionsImplemented: Bool {
135+
if _trailingSwipeActions != nil {
136+
return true
137+
}
138+
139+
return cellDescriptors.values.contains(where: { ($0 as? CellDescriptorTypeiOS11)?.trailingSwipeActionsClosure != nil })
103140
}
104141

105142
// MARK: Additional
@@ -309,23 +346,31 @@ public class DataSource: NSObject {
309346
extension DataSource {
310347
public var leadingSwipeActions: ((RowType, IndexPath) -> UISwipeActionsConfiguration?)? {
311348
get {
349+
if _leadingSwipeActions == nil {
350+
return nil
351+
}
352+
312353
return { [weak self] (rowType, indexPath) in
313-
return self?.leftSwipeActions?(rowType, indexPath) as? UISwipeActionsConfiguration
354+
return self?._leadingSwipeActions?(rowType, indexPath) as? UISwipeActionsConfiguration
314355
}
315356
}
316357
set {
317-
leftSwipeActions = newValue
358+
_leadingSwipeActions = newValue
318359
}
319360
}
320361

321362
public var trailingSwipeActions: ((RowType, IndexPath) -> UISwipeActionsConfiguration?)? {
322363
get {
364+
if _trailingSwipeActions == nil {
365+
return nil
366+
}
367+
323368
return { [weak self] (rowType, indexPath) in
324-
return self?.rightSwipeActions?(rowType, indexPath) as? UISwipeActionsConfiguration
369+
return self?._trailingSwipeActions?(rowType, indexPath) as? UISwipeActionsConfiguration
325370
}
326371
}
327372
set {
328-
rightSwipeActions = newValue
373+
_trailingSwipeActions = newValue
329374
}
330375
}
331376

Example/ViewControllers/Examples/SwipeToDeleteViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SwipeToDeleteViewController: UITableViewController {
3737
override func viewDidLoad() {
3838
super.viewDidLoad()
3939

40+
dataSource.fallbackDelegate = self
41+
4042
tableView.dataSource = dataSource
4143
tableView.delegate = dataSource
4244

Example/ViewControllers/SwipeActionViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class SwipeActionViewController: UITableViewController {
3030
self?.hapticFeedback.impactOccurred()
3131
callback(true)
3232
})
33-
])
33+
])
3434
}.trailingSwipeAction { [weak self] (_, indexPath) -> UISwipeActionsConfiguration? in
3535
return UISwipeActionsConfiguration(actions: [
3636
UIContextualAction(style: .normal, title: "Change Color", handler: { [weak self] (_, _, callback) in
3737
callback(true)
3838
self?.changeColor(for: indexPath)
3939
})
40-
])
40+
])
4141
}
4242
])
4343
}()

0 commit comments

Comments
 (0)