Skip to content

Commit c9ce689

Browse files
committed
ImmuTable: Add methods to allow reordering of table rows
1 parent 3c63070 commit c9ce689

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

WordPress/Classes/Utility/ImmuTable.swift

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import UIKit
12
/**
23
ImmuTable represents the view model for a static UITableView.
34

@@ -291,6 +292,26 @@ open class ImmuTableViewHandler: NSObject, UITableViewDataSource, UITableViewDel
291292
return viewModel.sections[section].footerText
292293
}
293294

295+
open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
296+
if target.responds(to: #selector(UITableViewDataSource.tableView(_:canEditRowAt:))) {
297+
return target.tableView?(tableView, canEditRowAt: indexPath) ?? false
298+
}
299+
300+
return false
301+
}
302+
303+
open func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
304+
if target.responds(to: #selector(UITableViewDataSource.tableView(_:canMoveRowAt:))) {
305+
return target.tableView?(tableView, canMoveRowAt: indexPath) ?? false
306+
}
307+
308+
return false
309+
}
310+
311+
open func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
312+
target.tableView?(tableView, moveRowAt: sourceIndexPath, to: destinationIndexPath)
313+
}
314+
294315
// MARK: UITableViewDelegate
295316

296317
open func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
@@ -352,14 +373,27 @@ open class ImmuTableViewHandler: NSObject, UITableViewDataSource, UITableViewDel
352373
return nil
353374
}
354375

355-
open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
356-
if target.responds(to: #selector(UITableViewDataSource.tableView(_:canEditRowAt:))) {
357-
return target.tableView?(tableView, canEditRowAt: indexPath) ?? false
376+
open func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
377+
if target.responds(to: #selector(UITableViewDelegate.tableView(_:targetIndexPathForMoveFromRowAt:toProposedIndexPath:))) {
378+
return target.tableView?(tableView, targetIndexPathForMoveFromRowAt: sourceIndexPath, toProposedIndexPath: proposedDestinationIndexPath) ?? proposedDestinationIndexPath
358379
}
359380

360-
return false
381+
return proposedDestinationIndexPath
361382
}
362383

384+
open func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
385+
if target.responds(to: #selector(UITableViewDelegate.tableView(_:editingStyleForRowAt:))) {
386+
return target.tableView?(tableView, editingStyleForRowAt: indexPath) ?? .none
387+
}
388+
389+
return .none
390+
}
391+
392+
open func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
393+
return target.tableView?(tableView, shouldIndentWhileEditingRowAt: indexPath) ?? true
394+
}
395+
396+
363397
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
364398
if target.responds(to: #selector(UITableViewDelegate.tableView(_:trailingSwipeActionsConfigurationForRowAt:))) {
365399
return target.tableView?(tableView, trailingSwipeActionsConfigurationForRowAt: indexPath)

0 commit comments

Comments
 (0)