質問編集履歴

6

コードの追加

2017/10/04 08:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -545,3 +545,33 @@
545
545
  @IBAction func addbtr
546
546
 
547
547
  のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックがつかないようにならないです。
548
+
549
+
550
+
551
+ ```
552
+
553
+ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
554
+
555
+
556
+
557
+ let cell = tableView.cellForRow(at: indexPath)
558
+
559
+
560
+
561
+ if indexPath.row == 0 {
562
+
563
+ return nil
564
+
565
+ }
566
+
567
+
568
+
569
+ if cell?.textLabel?.text != "cell 追加 ボタン"{
570
+
571
+ cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark
572
+
573
+ tableView.deselectRow(at: indexPath, animated: true)
574
+
575
+ return nil
576
+
577
+ ```

5

説明の追加

2017/10/04 08:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -544,4 +544,4 @@
544
544
 
545
545
  @IBAction func addbtr
546
546
 
547
- のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないです。
547
+ のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないです。

4

コードの追加

2017/10/04 02:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -510,6 +510,10 @@
510
510
 
511
511
 
512
512
 
513
+
514
+
515
+ **追記**
516
+
513
517
  ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
514
518
 
515
519
 
@@ -523,3 +527,21 @@
523
527
  self.kei.append("cell 追加 ボタン")
524
528
 
525
529
  ```
530
+
531
+
532
+
533
+ **試したこと**
534
+
535
+ ```
536
+
537
+ if cell?.textLabel?.text != "cell 追加 ボタン" && cell.textLabel?.text != "textField.text!" {
538
+
539
+
540
+
541
+ ```
542
+
543
+
544
+
545
+ @IBAction func addbtr
546
+
547
+ のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないのです。

3

コードの追加

2017/10/04 02:15

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -511,3 +511,15 @@
511
511
 
512
512
 
513
513
  ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
514
+
515
+
516
+
517
+ ```
518
+
519
+ @IBAction func addbtr(_ sender: Any) {
520
+
521
+    self.kei.append(textField.text!)
522
+
523
+ self.kei.append("cell 追加 ボタン")
524
+
525
+ ```

2

画像とコードの変更

2017/10/02 06:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,172 @@
1
1
  ```
2
2
 
3
+ import UIKit
4
+
5
+
6
+
7
+ class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
8
+
9
+
10
+
11
+ @IBOutlet weak var mytableView: UITableView!
12
+
13
+
14
+
15
+ var kei = [String]()//["cell 追加 ボタン"]
16
+
17
+
18
+
19
+ override func viewDidLoad() {
20
+
21
+ super.viewDidLoad()
22
+
23
+
24
+
25
+ mytableView.delegate = self
26
+
27
+ mytableView.dataSource = self
28
+
29
+ // trueで複数選択、falseで単一選択
30
+
31
+
32
+
33
+ }
34
+
35
+
36
+
37
+
38
+
39
+ @IBAction func addbtr(_ sender: Any) {
40
+
41
+
42
+
43
+
44
+
45
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
46
+
47
+
48
+
49
+ // OKボタンの設定
50
+
51
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
52
+
53
+ (action:UIAlertAction!) -> Void in
54
+
55
+
56
+
57
+ // OKを押した時入力されていたテキストを表示
58
+
59
+ if let textFields = alert.textFields {
60
+
61
+
62
+
63
+ // アラートに含まれるすべてのテキストフィールドを調べる
64
+
65
+ for textField in textFields {
66
+
67
+ self.kei.append(textField.text!) //insertは上に追加されappendは下に追加される
68
+
69
+ self.kei.append("cell 追加 ボタン")
70
+
71
+ }
72
+
73
+ self.mytableView.reloadData()
74
+
75
+
76
+
77
+ }
78
+
79
+ })
80
+
81
+ alert.addAction(okAction)
82
+
83
+
84
+
85
+ // キャンセルボタンの設定
86
+
87
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
88
+
89
+ alert.addAction(cancelAction)
90
+
91
+
92
+
93
+ // テキストフィールドを追加
94
+
95
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
96
+
97
+ textField.placeholder = "テキスト"
98
+
99
+ })
100
+
101
+
102
+
103
+
104
+
105
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
106
+
107
+
108
+
109
+ // アラートを画面に表示
110
+
111
+ self.present(alert, animated: true, completion: nil)
112
+
113
+
114
+
115
+ }
116
+
117
+
118
+
119
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
120
+
121
+ return kei.count
122
+
123
+ }
124
+
125
+
126
+
127
+
128
+
129
+ func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
130
+
131
+
132
+
133
+ switch indexPath.row {
134
+
135
+ // 選択不可にしたい場合は"nil"を返す
136
+
137
+ case 1:
138
+
139
+ return indexPath;
140
+
141
+
142
+
143
+ // 選択可にしたい場合は"indexPath"を返す
144
+
145
+ default:
146
+
147
+ return nil;
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
156
+
157
+ if kei[indexPath.row] != "cell 追加 ボタン" {
158
+
159
+ return nil
160
+
161
+ }
162
+
163
+ return indexPath
164
+
165
+ }
166
+
167
+
168
+
3
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
169
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
4
170
 
5
171
 
6
172
 
@@ -24,8 +190,6 @@
24
190
 
25
191
  cell.backgroundColor = UIColor.yellow
26
192
 
27
-
28
-
29
193
  }
30
194
 
31
195
  if indexPath.row == 0 {
@@ -54,6 +218,282 @@
54
218
 
55
219
 
56
220
 
221
+ if cell?.textLabel?.text != "cell 追加 ボタン" {
222
+
223
+ cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark
224
+
225
+ tableView.deselectRow(at: indexPath, animated: true)
226
+
227
+ return nil
228
+
229
+ }else {
230
+
231
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
232
+
233
+
234
+
235
+ // OKボタンの設定
236
+
237
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
238
+
239
+ (action:UIAlertAction!) -> Void in
240
+
241
+
242
+
243
+ // OKを押した時入力されていたテキストを表示
244
+
245
+ if let textFields = alert.textFields {
246
+
247
+
248
+
249
+ // アラートに含まれるすべてのテキストフィールドを調べる
250
+
251
+ for textField in textFields {
252
+
253
+ self.kei.insert(textField.text!, at: indexPath.row)//indexPath.rowで1,2,3,4のような順になる
254
+
255
+ //self.kei.insert(textField.text!, at:self.kei.count - 1)
256
+
257
+ }
258
+
259
+ self.mytableView.reloadData()
260
+
261
+
262
+
263
+ }
264
+
265
+ })
266
+
267
+ alert.addAction(okAction)
268
+
269
+
270
+
271
+ // キャンセルボタンの設定
272
+
273
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
274
+
275
+ alert.addAction(cancelAction)
276
+
277
+
278
+
279
+ // テキストフィールドを追加
280
+
281
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
282
+
283
+ textField.placeholder = "テキスト"
284
+
285
+ })
286
+
287
+
288
+
289
+
290
+
291
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
292
+
293
+
294
+
295
+ // アラートを画面に表示
296
+
297
+ self.present(alert, animated: true, completion: nil)
298
+
299
+
300
+
301
+ return indexPath
302
+
303
+ }
304
+
305
+
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+ //cellの削除について
314
+
315
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
316
+
317
+ if editingStyle == .delete {
318
+
319
+ self.kei.remove(at: indexPath.row)
320
+
321
+ tableView.deleteRows(at: [indexPath], with: .fade)
322
+
323
+ }
324
+
325
+ }
326
+
327
+
328
+
329
+ func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
330
+
331
+
332
+
333
+ let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
334
+
335
+ self.kei.remove(at: (indexPath.row))
336
+
337
+ tableView.deleteRows(at: [indexPath], with: .fade)
338
+
339
+ }
340
+
341
+ delete.backgroundColor = .red
342
+
343
+
344
+
345
+ let edit = UITableViewRowAction(style: .normal, title: "編集") { action, index in
346
+
347
+
348
+
349
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
350
+
351
+
352
+
353
+ // OKボタンの設定
354
+
355
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
356
+
357
+ (action:UIAlertAction!) -> Void in
358
+
359
+
360
+
361
+ // OKを押した時入力されていたテキストを表示
362
+
363
+ if let textFields = alert.textFields {
364
+
365
+
366
+
367
+ // アラートに含まれるすべてのテキストフィールドを調べる
368
+
369
+ for textField in textFields {
370
+
371
+ self.kei[indexPath.row] = textField.text!
372
+
373
+ //self.kei.append(textField.text!)
374
+
375
+ }
376
+
377
+ self.mytableView.reloadData()
378
+
379
+
380
+
381
+ }
382
+
383
+ })
384
+
385
+ alert.addAction(okAction)
386
+
387
+
388
+
389
+ // キャンセルボタンの設定
390
+
391
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
392
+
393
+ alert.addAction(cancelAction)
394
+
395
+
396
+
397
+ // テキストフィールドを追加
398
+
399
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
400
+
401
+ textField.placeholder = "テキスト"
402
+
403
+ })
404
+
405
+
406
+
407
+
408
+
409
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
410
+
411
+
412
+
413
+ // アラートを画面に表示
414
+
415
+ self.present(alert, animated: true, completion: nil)
416
+
417
+ print("edit")
418
+
419
+ }
420
+
421
+ edit.backgroundColor = .orange
422
+
423
+
424
+
425
+ return [delete,edit]
426
+
427
+ }
428
+
429
+
430
+
431
+ // trueを返すことでCellのアクションを許可しています
432
+
433
+ func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
434
+
435
+
436
+
437
+ if kei.count > (indexPath.row + 1) {
438
+
439
+ return true
440
+
441
+ }else{
442
+
443
+ return false
444
+
445
+ }
446
+
447
+ }
448
+
449
+
450
+
451
+ override func didReceiveMemoryWarning() {
452
+
453
+ super.didReceiveMemoryWarning()
454
+
455
+ // Dispose of any resources that can be recreated.
456
+
457
+ }
458
+
459
+ }
460
+
461
+ ```
462
+
463
+
464
+
465
+ **やりたい事**
466
+
467
+ [cell 追加 ボタン]とtitleのcell以外のcellをタップした時にチェックマークをつけたり、消したりできるようにしたいです
468
+
469
+
470
+
471
+ **今出来ているところ**
472
+
473
+ 一応cellを追加する時にチェックマークが勝手につくように出来ました
474
+
475
+
476
+
477
+ **困っているところ**
478
+
479
+ cellが追加された時にはまだチェックマークがついてない状態にしたいです。
480
+
481
+ それとつけることは出来たのですがタップした時にチェックマークを消せない状態にいます。
482
+
483
+ elseで消す処理は書いているのですが反応してくれません。
484
+
485
+ それで下記のようにしてみればと考えたのですがcell追加時には確かにcellが追加されなくはなったのですがそしたら今度はcellにチェックマークがつかなくなりました
486
+
487
+
488
+
489
+ **func didSelectRowAtIndexPathでこのようなコードを追加したところ[cell 追加 ボタン]だけcellが追加出来るようになりました**
490
+
491
+ ```
492
+
493
+ let cell = tableView.cellForRow(at: indexPath)
494
+
495
+
496
+
57
497
  if cell!.accessoryType == .checkmark{
58
498
 
59
499
  cell!.accessoryType = .none
@@ -64,138 +504,10 @@
64
504
 
65
505
  }
66
506
 
67
-
68
-
69
- if kei[indexPath.row] != "cell 追加 ボタン" {
507
+
70
-
71
- return nil
72
-
73
- }else {
74
-
75
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
76
-
77
-
78
-
79
- // OKボタンの設定
80
-
81
- let okAction = UIAlertAction(title: "OK", style: .default, handler: {
82
-
83
- (action:UIAlertAction!) -> Void in
84
-
85
-
86
-
87
- // OKを押した時入力されていたテキストを表示
88
-
89
- if let textFields = alert.textFields {
90
-
91
-
92
-
93
- // アラートに含まれるすべてのテキストフィールドを調べる
94
-
95
- for textField in textFields {
96
-
97
- self.kei.insert(textField.text!, at: indexPath.row)//indexPath.rowで1,2,3,4のような順になる
98
-
99
- //self.kei.insert(textField.text!, at:self.kei.count - 1)
100
-
101
- }
102
-
103
- self.mytableView.reloadData()
104
-
105
-
106
-
107
- }
108
-
109
- })
110
-
111
- alert.addAction(okAction)
112
-
113
-
114
-
115
- // キャンセルボタンの設定
116
-
117
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
118
-
119
- alert.addAction(cancelAction)
120
-
121
-
122
-
123
- // テキストフィールドを追加
124
-
125
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
126
-
127
- textField.placeholder = "テキスト"
128
-
129
- })
130
-
131
-
132
-
133
-
134
-
135
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
136
-
137
-
138
-
139
- // アラートを画面に表示
140
-
141
- self.present(alert, animated: true, completion: nil)
142
-
143
-
144
-
145
- return indexPath
146
-
147
- }
148
-
149
-
150
-
151
- }
152
508
 
153
509
  ```
154
510
 
155
511
 
156
512
 
157
- **やりたい事**
158
-
159
- [cell 追加 ボタン]とtitleのcell以外のcellをタップした時にチェックマークをつけたり、消したりできるようにしたいです
160
-
161
-
162
-
163
- **今出来ているところ**
164
-
165
- 一応cellを追加する時にチェックマークが勝手につくように出来ました
166
-
167
-
168
-
169
- **困っているところ**
170
-
171
- cellが追加された時にはまだチェックマークがついてない状態にしたいです。
172
-
173
- それとつけることは出来たのですがタップした時にチェックマークを消せない状態にいます。
174
-
175
- elseで消す処理は書いているのですが反応してくれません。
176
-
177
- それで下記のようにしてみればと考えたのですがcell追加時には確かにcellが追加されなくはなったのですがそしたら今度はcellにチェックマークがつかなくなりました
178
-
179
-
180
-
181
- **func didSelectRowAtIndexPathでこのようなコードを追加したところ[cell 追加 ボタン]だけcellが追加出来るようになりました**
182
-
183
- ```
184
-
185
- let cell = tableView.cellForRow(at: indexPath)
513
+ ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
186
-
187
-
188
-
189
- if cell!.accessoryType == .checkmark{
190
-
191
- cell!.accessoryType = .none
192
-
193
- }else{
194
-
195
- cell!.accessoryType = .checkmark
196
-
197
- }
198
-
199
-
200
-
201
- ```

1

コードの追加

2017/10/02 06:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,11 @@
24
24
 
25
25
  cell.backgroundColor = UIColor.yellow
26
26
 
27
+
28
+
27
29
  }
28
30
 
29
- if indexPath.row == 0 { //self.kei.append[String]() {
31
+ if indexPath.row == 0 {
30
32
 
31
33
  cell.textLabel?.textColor = UIColor.black
32
34
 
@@ -36,21 +38,117 @@
36
38
 
37
39
 
38
40
 
39
-
40
-
41
- if kei[indexPath.row] != "cell 追加 ボタン"{
42
-
43
- cell.accessoryType = .checkmark
41
+ return cell
42
+
43
+ }
44
+
45
+
46
+
47
+ /*if kei.count > (indexPath.row + 1)使うには func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ を func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {*/
48
+
49
+ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
50
+
51
+
52
+
53
+ let cell = tableView.cellForRow(at: indexPath)
54
+
55
+
56
+
57
+ if cell!.accessoryType == .checkmark{
58
+
59
+ cell!.accessoryType = .none
44
60
 
45
61
  }else{
46
62
 
47
- cell.accessoryType = .none
63
+ cell!.accessoryType = .checkmark
48
-
64
+
49
- }
65
+ }
66
+
67
+
68
+
50
-
69
+ if kei[indexPath.row] != "cell 追加 ボタン" {
70
+
51
- return cell
71
+ return nil
72
+
52
-
73
+ }else {
74
+
75
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
76
+
77
+
78
+
79
+ // OKボタンの設定
80
+
81
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
82
+
83
+ (action:UIAlertAction!) -> Void in
84
+
85
+
86
+
87
+ // OKを押した時入力されていたテキストを表示
88
+
89
+ if let textFields = alert.textFields {
90
+
91
+
92
+
93
+ // アラートに含まれるすべてのテキストフィールドを調べる
94
+
95
+ for textField in textFields {
96
+
97
+ self.kei.insert(textField.text!, at: indexPath.row)//indexPath.rowで1,2,3,4のような順になる
98
+
99
+ //self.kei.insert(textField.text!, at:self.kei.count - 1)
100
+
53
- }
101
+ }
102
+
103
+ self.mytableView.reloadData()
104
+
105
+
106
+
107
+ }
108
+
109
+ })
110
+
111
+ alert.addAction(okAction)
112
+
113
+
114
+
115
+ // キャンセルボタンの設定
116
+
117
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
118
+
119
+ alert.addAction(cancelAction)
120
+
121
+
122
+
123
+ // テキストフィールドを追加
124
+
125
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
126
+
127
+ textField.placeholder = "テキスト"
128
+
129
+ })
130
+
131
+
132
+
133
+
134
+
135
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
136
+
137
+
138
+
139
+ // アラートを画面に表示
140
+
141
+ self.present(alert, animated: true, completion: nil)
142
+
143
+
144
+
145
+ return indexPath
146
+
147
+ }
148
+
149
+
150
+
151
+ }
54
152
 
55
153
  ```
56
154
 
@@ -80,28 +178,24 @@
80
178
 
81
179
 
82
180
 
181
+ **func didSelectRowAtIndexPathでこのようなコードを追加したところ[cell 追加 ボタン]だけcellが追加出来るようになりました**
182
+
83
- ```
183
+ ```
84
-
184
+
85
- if kei[indexPath.row] != "cell 追加 ボタン"{
185
+ let cell = tableView.cellForRow(at: indexPath)
186
+
187
+
188
+
86
-
189
+ if cell!.accessoryType == .checkmark{
190
+
87
- cell.accessoryType = .none
191
+ cell!.accessoryType = .none
88
-
89
- }else if kei.count > (indexPath.row + 1){
90
-
91
- cell.accessoryType = .checkmark
92
192
 
93
193
  }else{
94
194
 
95
- cell.accessoryType = .none
195
+ cell!.accessoryType = .checkmark
96
-
196
+
97
- }
197
+ }
98
-
99
- return cell
198
+
100
-
101
-
102
-
199
+
200
+
103
- ```
201
+ ```
104
-
105
-
106
-
107
- ![イメージ説明](4acc42ff41c20630da7665d53632d4a9.png)