回答編集履歴
1
0
test
CHANGED
@@ -19,3 +19,145 @@
|
|
19
19
|
こっちはarrayの中身をsourceIndexPath.rowからdestinationIndexPath.rowへ移動させるようなコードを自分で書かないといけないです。
|
20
20
|
|
21
21
|
中に何の値が入っているかprintなどして、どうすればいいか考えて実装してください。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
----
|
28
|
+
|
29
|
+
一応手元でも動かしてみましたが適当に作ったコードでもドラッグ中のものは勝手に表示されているようです。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```swift
|
34
|
+
|
35
|
+
import UIKit
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
class ViewController: UIViewController, UITableViewDataSource, UITableViewDragDelegate, UITableViewDropDelegate {
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
let tableView = UITableView(frame: .zero, style: .plain)
|
44
|
+
|
45
|
+
let array = ["あああ", "いいい", "ううう", "えええ", "おおお", "かかか", "ききき"]
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
override func viewDidLoad() {
|
50
|
+
|
51
|
+
super.viewDidLoad()
|
52
|
+
|
53
|
+
// Do any additional setup after loading the view.
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
tableView.frame = UIScreen.main.bounds
|
60
|
+
|
61
|
+
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
|
62
|
+
|
63
|
+
tableView.dataSource = self
|
64
|
+
|
65
|
+
tableView.dragDelegate = self
|
66
|
+
|
67
|
+
tableView.dropDelegate = self
|
68
|
+
|
69
|
+
tableView.dragInteractionEnabled = true
|
70
|
+
|
71
|
+
view.addSubview(tableView)
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// MARK: - UITableViewDataSource
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
82
|
+
|
83
|
+
return array.count
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
90
|
+
|
91
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
92
|
+
|
93
|
+
cell.textLabel?.text = array[indexPath.row]
|
94
|
+
|
95
|
+
//cell.layer.backgroundColor = UIColor.clear.cgColor
|
96
|
+
|
97
|
+
return cell
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// MARK: - UITableViewDataSource
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
108
|
+
|
109
|
+
return [UIDragItem(itemProvider: NSItemProvider())]
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
// MARK: - UITableViewDropDelegate
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession,
|
120
|
+
|
121
|
+
withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
|
122
|
+
|
123
|
+
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
|
130
|
+
|
131
|
+
guard let item = coordinator.items.first,
|
132
|
+
|
133
|
+
let destinationIndexPath = coordinator.destinationIndexPath,
|
134
|
+
|
135
|
+
let sourceIndexPath = item.sourceIndexPath else { return }
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
/*tableView.performBatchUpdates({ [weak self] in
|
140
|
+
|
141
|
+
guard let strongSelf = self else { return }
|
142
|
+
|
143
|
+
strongSelf.resource.moveItem(sourcePath: sourceIndexPath.row, destinationPath: destinationIndexPath.row)
|
144
|
+
|
145
|
+
tableView.deleteRows(at: [sourceIndexPath], with: .automatic)
|
146
|
+
|
147
|
+
tableView.insertRows(at: [destinationIndexPath], with: .automatic)
|
148
|
+
|
149
|
+
}, completion: nil)*/
|
150
|
+
|
151
|
+
coordinator.drop(item.dragItem, toRowAt: destinationIndexPath)
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
実行結果:
|
162
|
+
|
163
|
+
![イメージ説明](c24959af4f93f99741d0534addf74fb1.png)
|