質問編集履歴

3

こードの追加

2017/09/23 23:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -278,13 +278,11 @@
278
278
 
279
279
 
280
280
 
281
+
282
+
281
283
  ```
282
284
 
283
- //cellの削除について
284
-
285
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
285
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
286
-
287
- let swipeCellA = UITableViewRowAction(style: .default, title: "削除") { action, index in
288
286
 
289
287
  if editingStyle == .delete {
290
288
 
@@ -292,94 +290,128 @@
292
290
 
293
291
  tableView.deleteRows(at: [indexPath], with: .fade)
294
292
 
295
- }
296
-
297
293
  }
298
294
 
295
+ }
296
+
297
+ ```
298
+
299
+
300
+
301
+ ```
302
+
303
+ func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
304
+
299
- let swipeCellB = UITableViewRowAction(style: .default, title: "編集") { action, index in
305
+ let more = UITableViewRowAction(style: .normal, title: "削除") { action, index in
306
+
307
+
308
+
309
+ self.kei.remove(at: (indexPath.row))
310
+
311
+ tableView.deleteRows(at: [indexPath], with: .fade)
312
+
313
+ print("more button tapped")
300
314
 
301
315
 
302
316
 
317
+ }
318
+
303
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
319
+ more.backgroundColor = .lightGray
304
320
 
305
321
 
306
322
 
323
+ let favorite = UITableViewRowAction(style: .normal, title: "編集") { action, index in
324
+
325
+
326
+
327
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
328
+
329
+
330
+
307
- // OKボタンの設定
331
+ // OKボタンの設定
308
-
332
+
309
- let okAction = UIAlertAction(title: "OK", style: .default, handler: {
333
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
310
-
334
+
311
- (action:UIAlertAction!) -> Void in
335
+ (action:UIAlertAction!) -> Void in
312
-
313
-
314
-
315
- // OKを押した時入力されていたテキストを表示
316
-
317
- if let textFields = alert.textFields {
318
336
 
319
337
 
320
338
 
339
+ // OKを押した時入力されていたテキストを表示
340
+
341
+ if let textFields = alert.textFields {
342
+
343
+
344
+
321
- // アラートに含まれるすべてのテキストフィールドを調べる
345
+ // アラートに含まれるすべてのテキストフィールドを調べる
322
-
346
+
323
- for textField in textFields {
347
+ for textField in textFields {
324
-
348
+
325
- self.kei.insert(textField.text!, at:0)
349
+ self.kei.insert(textField.text!, at:0)
326
-
350
+
327
- //self.kei.append(textField.text!)
351
+ //self.kei.append(textField.text!)
352
+
353
+ }
354
+
355
+ self.mytableView.reloadData()
356
+
357
+
328
358
 
329
359
  }
330
360
 
361
+ })
362
+
363
+ alert.addAction(okAction)
364
+
365
+
366
+
367
+ // キャンセルボタンの設定
368
+
369
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
370
+
331
- self.mytableView.reloadData()
371
+ alert.addAction(cancelAction)
372
+
373
+
374
+
332
-
375
+ // テキストフィールドを追加
376
+
333
-
377
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
378
+
334
-
379
+ textField.placeholder = "テキスト"
380
+
381
+ })
382
+
383
+
384
+
385
+
386
+
387
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
388
+
389
+
390
+
391
+ // アラートを画面に表示
392
+
393
+ self.present(alert, animated: true, completion: nil)
394
+
395
+
396
+
397
+ print("favorite button tapped")
398
+
335
- }
399
+ }
336
-
337
- })
400
+
338
-
339
- alert.addAction(okAction)
401
+ favorite.backgroundColor = .orange
340
402
 
341
403
 
342
404
 
343
- // キャンセルボタンの設定
344
-
345
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
346
-
347
- alert.addAction(cancelAction)
405
+ return [favorite, more]
348
-
349
-
350
-
351
- // テキストフィールドを追加
352
-
353
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
354
-
355
- textField.placeholder = "テキスト"
356
-
357
- })
358
-
359
-
360
-
361
-
362
-
363
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
364
-
365
-
366
-
367
- // アラートを画面に表示
368
-
369
- self.present(alert, animated: true, completion: nil)
370
406
 
371
407
  }
372
408
 
373
-
374
-
375
- swipeCellA.backgroundColor = UIColor.red
409
+
376
-
377
- swipeCellB.backgroundColor = UIColor.blue
378
-
379
-
380
-
381
- }
382
410
 
383
411
  ```
384
412
 
385
- このようにして見たのですが、スワイプするとデリートボタンはてくるのですが、編集ボタンが出てこくて困ってます。どのようにすれば上記のような感じに出来るのでしょうか?
413
+ **ないこと**
414
+
415
+ ```
416
+
417
+ func editingStyle```を使うとスワイプして削除はできるのですが、スワイプした時に削除ボタンの他に編集ボタンを加えようと```func editingStyle```のコードを消して```func editActionsForRowAt```に変えてコードを書いて見たのですがそうすると```func editingStyle```では出来たスワイプしての削除でさえ出来なくなりました。

2

コードの追加

2017/09/23 23:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -275,3 +275,111 @@
275
275
  イメージはこんな感じです。
276
276
 
277
277
  ![イメージ説明](ea96db7fb23c2d27f2556fbd0da38fc8.png)
278
+
279
+
280
+
281
+ ```
282
+
283
+ //cellの削除について
284
+
285
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
286
+
287
+ let swipeCellA = UITableViewRowAction(style: .default, title: "削除") { action, index in
288
+
289
+ if editingStyle == .delete {
290
+
291
+ self.kei.remove(at: indexPath.row)
292
+
293
+ tableView.deleteRows(at: [indexPath], with: .fade)
294
+
295
+ }
296
+
297
+ }
298
+
299
+ let swipeCellB = UITableViewRowAction(style: .default, title: "編集") { action, index in
300
+
301
+
302
+
303
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
304
+
305
+
306
+
307
+ // OKボタンの設定
308
+
309
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
310
+
311
+ (action:UIAlertAction!) -> Void in
312
+
313
+
314
+
315
+ // OKを押した時入力されていたテキストを表示
316
+
317
+ if let textFields = alert.textFields {
318
+
319
+
320
+
321
+ // アラートに含まれるすべてのテキストフィールドを調べる
322
+
323
+ for textField in textFields {
324
+
325
+ self.kei.insert(textField.text!, at:0)
326
+
327
+ //self.kei.append(textField.text!)
328
+
329
+ }
330
+
331
+ self.mytableView.reloadData()
332
+
333
+
334
+
335
+ }
336
+
337
+ })
338
+
339
+ alert.addAction(okAction)
340
+
341
+
342
+
343
+ // キャンセルボタンの設定
344
+
345
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
346
+
347
+ alert.addAction(cancelAction)
348
+
349
+
350
+
351
+ // テキストフィールドを追加
352
+
353
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
354
+
355
+ textField.placeholder = "テキスト"
356
+
357
+ })
358
+
359
+
360
+
361
+
362
+
363
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
364
+
365
+
366
+
367
+ // アラートを画面に表示
368
+
369
+ self.present(alert, animated: true, completion: nil)
370
+
371
+ }
372
+
373
+
374
+
375
+ swipeCellA.backgroundColor = UIColor.red
376
+
377
+ swipeCellB.backgroundColor = UIColor.blue
378
+
379
+
380
+
381
+ }
382
+
383
+ ```
384
+
385
+ このようにして見たのですが、スワイプするとデリートボタンは出てくるのですが、編集ボタンが出てこなくて困っています。どのようにすれば上記のような感じに出来るのでしょうか?

1

書式の変更

2017/09/23 13:27

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -122,160 +122,116 @@
122
122
 
123
123
 
124
124
 
125
+ cell.textLabel?.text = kei[indexPath.row]
126
+
127
+
128
+
129
+ cell.textLabel?.textColor = UIColor.brown
130
+
131
+
132
+
133
+ cell.textLabel?.backgroundColor = UIColor.blue
134
+
135
+
136
+
137
+ cell.textLabel?.font = UIFont.systemFont(ofSize: 30)
138
+
139
+
140
+
141
+ return cell
142
+
143
+ }
144
+
145
+
146
+
147
+ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){
148
+
149
+
150
+
125
- //let cell2 : UITableView = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
151
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
152
+
153
+
154
+
155
+
156
+
126
-
157
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
158
+
127
- if indexPath.row == 1 {
159
+ (action:UIAlertAction!) -> Void in
128
160
 
129
161
 
130
162
 
131
- cell.selectionStyle = UITableViewCellSelectionStyle.blue
132
-
133
- }else {
134
-
135
163
 
136
164
 
165
+ if let textFields = alert.textFields {
166
+
167
+
168
+
169
+
170
+
171
+ for textField in textFields {
172
+
173
+ self.kei.insert(textField.text!, at:0)
174
+
175
+ //self.kei.append(textField.text!)
176
+
177
+ }
178
+
179
+ self.mytableView.reloadData()
180
+
181
+
182
+
183
+ }
184
+
185
+ })
186
+
187
+ alert.addAction(okAction)
188
+
189
+
190
+
191
+
192
+
193
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
194
+
195
+ alert.addAction(cancelAction)
196
+
197
+
198
+
199
+
200
+
201
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
202
+
203
+ textField.placeholder = "テキスト"
204
+
205
+ })
206
+
207
+
208
+
209
+
210
+
211
+ alert.view.setNeedsLayout()
212
+
213
+
214
+
137
- cell.selectionStyle = UITableViewCellSelectionStyle.none
215
+ self.present(alert, animated: true, completion: nil)
216
+
217
+
218
+
219
+ }
220
+
221
+
222
+
223
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
224
+
225
+ if editingStyle == .delete {
226
+
227
+ kei.remove(at: indexPath.row)
228
+
229
+ tableView.deleteRows(at: [indexPath], with: .fade)
138
230
 
139
231
  }
140
232
 
141
233
 
142
234
 
143
- cell.textLabel?.text = kei[indexPath.row]
144
-
145
-
146
-
147
- cell.textLabel?.textColor = UIColor.brown
148
-
149
-
150
-
151
- cell.textLabel?.backgroundColor = UIColor.blue
152
-
153
-
154
-
155
- cell.textLabel?.font = UIFont.systemFont(ofSize: 30)
156
-
157
-
158
-
159
- return cell
160
-
161
- }
162
-
163
-
164
-
165
- func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
166
-
167
-
168
-
169
- switch indexPath.row {
170
-
171
- case 0:
172
-
173
- return indexPath;
174
-
175
- case 1:
176
-
177
- return nil;
178
-
179
- default:
180
-
181
- return indexPath;
182
-
183
- }
184
-
185
- }
186
-
187
-
188
-
189
-
190
-
191
- func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){
192
-
193
-
194
-
195
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
196
-
197
-
198
-
199
-
200
-
201
- let okAction = UIAlertAction(title: "OK", style: .default, handler: {
202
-
203
- (action:UIAlertAction!) -> Void in
204
-
205
-
206
-
207
-
208
-
209
- if let textFields = alert.textFields {
210
-
211
-
212
-
213
-
214
-
215
- for textField in textFields {
216
-
217
- self.kei.insert(textField.text!, at:0)
218
-
219
- //self.kei.append(textField.text!)
220
-
221
- }
222
-
223
- self.mytableView.reloadData()
224
-
225
-
226
-
227
- }
228
-
229
- })
230
-
231
- alert.addAction(okAction)
232
-
233
-
234
-
235
-
236
-
237
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
238
-
239
- alert.addAction(cancelAction)
240
-
241
-
242
-
243
-
244
-
245
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
246
-
247
- textField.placeholder = "テキスト"
248
-
249
- })
250
-
251
-
252
-
253
-
254
-
255
- alert.view.setNeedsLayout()
256
-
257
-
258
-
259
- self.present(alert, animated: true, completion: nil)
260
-
261
-
262
-
263
- }
264
-
265
-
266
-
267
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
268
-
269
- if editingStyle == .delete {
270
-
271
- kei.remove(at: indexPath.row)
272
-
273
- tableView.deleteRows(at: [indexPath], with: .fade)
274
-
275
- }
276
-
277
-
278
-
279
235
  }
280
236
 
281
237