回答編集履歴
1
0
answer
CHANGED
@@ -8,4 +8,75 @@
|
|
8
8
|
> strongSelf.array.moveItem(sourcePath: sourceIndexPath.row, destinationPath: destinationIndexPath.row)
|
9
9
|
|
10
10
|
こっちはarrayの中身をsourceIndexPath.rowからdestinationIndexPath.rowへ移動させるようなコードを自分で書かないといけないです。
|
11
|
-
中に何の値が入っているかprintなどして、どうすればいいか考えて実装してください。
|
11
|
+
中に何の値が入っているかprintなどして、どうすればいいか考えて実装してください。
|
12
|
+
|
13
|
+
|
14
|
+
----
|
15
|
+
一応手元でも動かしてみましたが適当に作ったコードでもドラッグ中のものは勝手に表示されているようです。
|
16
|
+
|
17
|
+
```swift
|
18
|
+
import UIKit
|
19
|
+
|
20
|
+
class ViewController: UIViewController, UITableViewDataSource, UITableViewDragDelegate, UITableViewDropDelegate {
|
21
|
+
|
22
|
+
let tableView = UITableView(frame: .zero, style: .plain)
|
23
|
+
let array = ["あああ", "いいい", "ううう", "えええ", "おおお", "かかか", "ききき"]
|
24
|
+
|
25
|
+
override func viewDidLoad() {
|
26
|
+
super.viewDidLoad()
|
27
|
+
// Do any additional setup after loading the view.
|
28
|
+
|
29
|
+
|
30
|
+
tableView.frame = UIScreen.main.bounds
|
31
|
+
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
|
32
|
+
tableView.dataSource = self
|
33
|
+
tableView.dragDelegate = self
|
34
|
+
tableView.dropDelegate = self
|
35
|
+
tableView.dragInteractionEnabled = true
|
36
|
+
view.addSubview(tableView)
|
37
|
+
}
|
38
|
+
|
39
|
+
// MARK: - UITableViewDataSource
|
40
|
+
|
41
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
42
|
+
return array.count
|
43
|
+
}
|
44
|
+
|
45
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
46
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
47
|
+
cell.textLabel?.text = array[indexPath.row]
|
48
|
+
//cell.layer.backgroundColor = UIColor.clear.cgColor
|
49
|
+
return cell
|
50
|
+
}
|
51
|
+
|
52
|
+
// MARK: - UITableViewDataSource
|
53
|
+
|
54
|
+
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
55
|
+
return [UIDragItem(itemProvider: NSItemProvider())]
|
56
|
+
}
|
57
|
+
|
58
|
+
// MARK: - UITableViewDropDelegate
|
59
|
+
|
60
|
+
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession,
|
61
|
+
withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
|
62
|
+
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
|
63
|
+
}
|
64
|
+
|
65
|
+
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
|
66
|
+
guard let item = coordinator.items.first,
|
67
|
+
let destinationIndexPath = coordinator.destinationIndexPath,
|
68
|
+
let sourceIndexPath = item.sourceIndexPath else { return }
|
69
|
+
|
70
|
+
/*tableView.performBatchUpdates({ [weak self] in
|
71
|
+
guard let strongSelf = self else { return }
|
72
|
+
strongSelf.resource.moveItem(sourcePath: sourceIndexPath.row, destinationPath: destinationIndexPath.row)
|
73
|
+
tableView.deleteRows(at: [sourceIndexPath], with: .automatic)
|
74
|
+
tableView.insertRows(at: [destinationIndexPath], with: .automatic)
|
75
|
+
}, completion: nil)*/
|
76
|
+
coordinator.drop(item.dragItem, toRowAt: destinationIndexPath)
|
77
|
+
}
|
78
|
+
}
|
79
|
+
```
|
80
|
+
|
81
|
+
実行結果:
|
82
|
+

|