質問編集履歴

1

指摘箇所を修正いたしました。

2019/12/05 08:16

投稿

tatsu757
tatsu757

スコア12

test CHANGED
@@ -1 +1 @@
1
- TableViewCellのドラッグアンドドロップ
1
+ TableViewCellのドラッグアンドドロップがうまくいかない
test CHANGED
@@ -12,243 +12,239 @@
12
12
 
13
13
 
14
14
 
15
+
16
+
17
+
18
+
19
+ ### 該当のソースコード
20
+
21
+
22
+
23
+ ```swift
24
+
25
+ import UIKit
26
+
27
+
28
+
29
+ class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UITableViewDragDelegate,UITableViewDropDelegate {
30
+
31
+
32
+
33
+
34
+
35
+ //セクションの数を指定
36
+
37
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
38
+
39
+
40
+
41
+ return array.count
42
+
43
+ }
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+ //生成したセルを順番に表示
52
+
53
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
54
+
55
+
56
+
57
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
58
+
59
+ //Table View CellのidentifireをCellとする
60
+
61
+
62
+
63
+ cell.textLabel?.text = array[indexPath.row]
64
+
65
+ cell.layer.backgroundColor = UIColor.clear.cgColor
66
+
67
+ return cell
68
+
69
+ }
70
+
71
+
72
+
73
+ //セルの移動許可
74
+
75
+ func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
76
+
77
+ return true
78
+
79
+ }
80
+
81
+
82
+
83
+ //セルを移動した時の処理
84
+
85
+ func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
86
+
87
+ // TODO: 入れ替え時の処理を実装する(データ制御など)
88
+
89
+
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ @IBOutlet var tableView: UITableView!
102
+
103
+
104
+
105
+ //todoを入れる配列
106
+
107
+
108
+
109
+ var array = [String]()
110
+
111
+
112
+
113
+ override func viewDidLoad() {
114
+
115
+ super.viewDidLoad()
116
+
117
+ tableView.delegate = self
118
+
119
+ tableView.dataSource = self
120
+
121
+ tableView.dragDelegate = self
122
+
123
+ tableView.dropDelegate = self
124
+
125
+ tableView.dragInteractionEnabled = true
126
+
127
+ //現在の配列を取り出す
128
+
129
+ if (UserDefaults.standard.object(forKey: "todo") != nil){
130
+
131
+ array = UserDefaults.standard.object(forKey: "todo") as! [String]
132
+
133
+ }
134
+
135
+
136
+
137
+ self.tableView.reloadData()
138
+
139
+
140
+
141
+ }
142
+
143
+
144
+
145
+ //セルのドラッグ処理
146
+
147
+ func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
148
+
149
+ //?????
150
+
151
+ return [?????.dragItem (for: indexPath)]
152
+
153
+ }
154
+
155
+
156
+
157
+
158
+
159
+ func dragItem(for indexPath: IndexPath) -> UIDragItem {
160
+
161
+ //?????
162
+
163
+ let task = array[indexPath.row]
164
+
165
+ let itemProvider = NSItemProvider(object: task as NSString)
166
+
167
+
168
+
169
+ return UIDragItem(itemProvider: NSItemProvider())
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+ //セルのドロップ
178
+
179
+ func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession,
180
+
181
+ withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
182
+
183
+ return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
184
+
185
+ }
186
+
187
+
188
+
189
+ func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
190
+
191
+ guard let item = coordinator.items.first,
192
+
193
+ let destinationIndexPath = coordinator.destinationIndexPath,
194
+
195
+ let sourceIndexPath = item.sourceIndexPath else { return }
196
+
197
+
198
+
199
+ tableView.performBatchUpdates({ [weak self] in
200
+
201
+ guard let strongSelf = self else { return }
202
+
203
+ //????
204
+
205
+ strongSelf.array.moveItem(sourcePath: sourceIndexPath.row, destinationPath: destinationIndexPath.row)
206
+
207
+ tableView.deleteRows(at: [sourceIndexPath], with: .automatic)
208
+
209
+ tableView.insertRows(at: [destinationIndexPath], with: .automatic)
210
+
211
+ }, completion: nil)
212
+
213
+ coordinator.drop(item.dragItem, toRowAt: destinationIndexPath)
214
+
215
+ }
216
+
217
+
218
+
219
+
220
+
221
+ func moveItem(sourcePath: Int, destinationPath: Int) {
222
+
223
+ let moveTask = array.remove(at: sourcePath)
224
+
225
+ array.insert(moveTask, at: destinationPath)
226
+
227
+ }
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+ //ここから下のコードはうまく動いている↓
236
+
15
237
  ```
16
238
 
17
- エラーメッセージ
18
-
19
- ```
20
-
21
-
22
-
23
- ### 該当のソースコード
24
-
25
-
26
-
27
- ```swift
28
-
29
- import UIKit
30
-
31
-
32
-
33
- class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UITableViewDragDelegate,UITableViewDropDelegate {
34
-
35
-
36
-
37
-
38
-
39
- //セクションの数を指定
40
-
41
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
42
-
43
-
44
-
45
- return array.count
46
-
47
- }
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
- //生成したセルを順番に表示
56
-
57
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
58
-
59
-
60
-
61
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
62
-
63
- //Table View CellのidentifireをCellとする
64
-
65
-
66
-
67
- cell.textLabel?.text = array[indexPath.row]
68
-
69
- cell.layer.backgroundColor = UIColor.clear.cgColor
70
-
71
- return cell
72
-
73
- }
74
-
75
-
76
-
77
- //セルの移動許可
78
-
79
- func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
80
-
81
- return true
82
-
83
- }
84
-
85
-
86
-
87
- //セルを移動した時の処理
88
-
89
- func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
90
-
91
- // TODO: 入れ替え時の処理を実装する(データ制御など)
92
-
93
-
94
-
95
- }
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
- @IBOutlet var tableView: UITableView!
106
-
107
-
108
-
109
- //todoを入れる配列
110
-
111
-
112
-
113
- var array = [String]()
114
-
115
-
116
-
117
- override func viewDidLoad() {
118
-
119
- super.viewDidLoad()
120
-
121
- tableView.delegate = self
122
-
123
- tableView.dataSource = self
124
-
125
- tableView.dragDelegate = self
126
-
127
- tableView.dropDelegate = self
128
-
129
- tableView.dragInteractionEnabled = true
130
-
131
- //現在の配列を取り出す
132
-
133
- if (UserDefaults.standard.object(forKey: "todo") != nil){
134
-
135
- array = UserDefaults.standard.object(forKey: "todo") as! [String]
136
-
137
- }
138
-
139
-
140
-
141
- self.tableView.reloadData()
142
-
143
-
144
-
145
- }
146
-
147
-
148
-
149
- //セルのドラッグ処理
150
-
151
- func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
152
-
153
- //?????
154
-
155
- return [?????.dragItem (for: indexPath)]
156
-
157
- }
158
-
159
-
160
-
161
-
162
-
163
- func dragItem(for indexPath: IndexPath) -> UIDragItem {
164
-
165
- //?????
166
-
167
- let task = array[indexPath.row]
168
-
169
- let itemProvider = NSItemProvider(object: task as NSString)
170
-
171
-
172
-
173
- return UIDragItem(itemProvider: NSItemProvider())
174
-
175
- }
176
-
177
-
178
-
179
-
180
-
181
- //セルのドロップ
182
-
183
- func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession,
184
-
185
- withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
186
-
187
- return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
188
-
189
- }
190
-
191
-
192
-
193
- func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
194
-
195
- guard let item = coordinator.items.first,
196
-
197
- let destinationIndexPath = coordinator.destinationIndexPath,
198
-
199
- let sourceIndexPath = item.sourceIndexPath else { return }
200
-
201
-
202
-
203
- tableView.performBatchUpdates({ [weak self] in
204
-
205
- guard let strongSelf = self else { return }
206
-
207
- //????
208
-
209
- strongSelf.array.moveItem(sourcePath: sourceIndexPath.row, destinationPath: destinationIndexPath.row)
210
-
211
- tableView.deleteRows(at: [sourceIndexPath], with: .automatic)
212
-
213
- tableView.insertRows(at: [destinationIndexPath], with: .automatic)
214
-
215
- }, completion: nil)
216
-
217
- coordinator.drop(item.dragItem, toRowAt: destinationIndexPath)
218
-
219
- }
220
-
221
-
222
-
223
-
224
-
225
- func moveItem(sourcePath: Int, destinationPath: Int) {
226
-
227
- let moveTask = array.remove(at: sourcePath)
228
-
229
- array.insert(moveTask, at: destinationPath)
230
-
231
- }
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
- //ここから下のコードはうまく動いている↓
240
-
241
- ```
242
-
243
239
 
244
240
 
245
241
  ### 試したこと
246
242
 
247
243
 
248
244
 
249
- 参考にしたサイト
245
+ 参考サイト
250
-
246
+
251
- https://dev.classmethod.jp/smartphone/iphone/uitableview-dragdelegate-dropdelegate1/
247
+ [UITableViewのdragDelegate, dropDelegateについて調べて試した](https://dev.classmethod.jp/smartphone/iphone/uitableview-dragdelegate-dropdelegate1/)
252
248
 
253
249
 
254
250