Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // Demonstrating a bug with what the table view reports as the destination index path when dragging within
  2. // the same section
  3.  
  4. import UIKit
  5.  
  6. class TableViewController: UITableViewController, UITableViewDragDelegate, UITableViewDropDelegate {
  7. var data: [(String, [String])] = [("One", ["A", "B", "C"]), ("Two", ["1", "2", "3"])]
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
  12. tableView.dragInteractionEnabled = true
  13. tableView.dragDelegate = self
  14. tableView.dropDelegate = self
  15. }
  16.  
  17. override func numberOfSections(in tableView: UITableView) -> Int {
  18. return data.count
  19. }
  20.  
  21. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  22. return data[section].1.count
  23. }
  24.  
  25. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  26. let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
  27. cell.textLabel?.text = data[indexPath.section].1[indexPath.row]
  28. return cell
  29. }
  30.  
  31. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  32. return data[section].0
  33. }
  34.  
  35. // DRAG
  36.  
  37. func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  38. return [UIDragItem(itemProvider: NSItemProvider(object: DragItem(data[indexPath.section].1[indexPath.row])))]
  39. }
  40.  
  41. // DROP
  42.  
  43. func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
  44. guard let indexPath = destinationIndexPath else {
  45. info("Forbidden")
  46. return UITableViewDropProposal(operation: .forbidden)
  47. }
  48.  
  49. // When reordering within the same section, unexpected results are shown here.
  50.  
  51. info("Insert at \(indexPath)?")
  52. return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  53. }
  54.  
  55. func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
  56. guard let indexPath = coordinator.destinationIndexPath else {
  57. info("Nowhere to drop")
  58. return
  59. }
  60. info("Dropping at \(indexPath)")
  61. }
  62.  
  63. func info(_ string: String) {
  64. navigationItem.title = string
  65. }
  66. }
  67.  
  68. class DragItem: NSObject, NSItemProviderWriting {
  69. static var writableTypeIdentifiersForItemProvider: [String] = ["tech.skagedal.test"]
  70.  
  71. func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
  72. return nil
  73. }
  74.  
  75. let string: String
  76. init(_ string: String) {
  77. self.string = string
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement