質問編集履歴

6

文法の修正

2017/09/29 15:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -439,11 +439,3 @@
439
439
  前までは**+**ボタンからでもアラートテキストで入力した内容がcellに追加できたのですが色々コードを書き足している内に出来なくなりました。
440
440
 
441
441
  それと[cell 追加ボタン]がどういう仕組みでtableViewに表示されているのかが分からないので**+**ボタンの中に[cell 追加ボタン]の処理の書き方が分からなく困っています。
442
-
443
-
444
-
445
- ![イメージ説明](36d2b852c43bc5cf9a03b9e5fa3dbec6.png)
446
-
447
-
448
-
449
- ![イメージ説明](16b8b6cd19bdb9f135f44cdd58937c30.png)

5

全コードの追加

2017/09/29 15:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,9 @@
12
12
 
13
13
 
14
14
 
15
- var kei = ["cell 追加 ボタン"]
15
+ var kei = [String]()
16
+
17
+
16
18
 
17
19
 
18
20
 
@@ -34,37 +36,35 @@
34
36
 
35
37
  }
36
38
 
37
- @IBOutlet weak var addbtr: UIBarButtonItem!
39
+
38
-
40
+
41
+
42
+
39
- @IBAction func addbutton(_ sender: Any) {
43
+ @IBAction func addbtr(_ sender: Any) {
40
-
41
-
42
-
44
+
45
+
46
+
47
+
48
+
43
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
49
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
44
-
45
-
46
-
47
- // OKボタンの設定
50
+
51
+
48
52
 
49
53
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
50
54
 
51
55
  (action:UIAlertAction!) -> Void in
52
56
 
53
-
54
-
55
- // OKを押した時入力されていたテキストを表示
57
+
56
58
 
57
59
  if let textFields = alert.textFields {
58
60
 
59
61
 
60
62
 
61
- // アラートに含まれるすべてのテキストフィールドを調べる
62
-
63
63
  for textField in textFields {
64
64
 
65
- self.kei.insert(textField.text!, at:0)
65
+ self.kei.append(textField.text!)
66
-
66
+
67
- //self.kei.append(textField.text!)
67
+ self.kei.append("cell 追加 ボタン")
68
68
 
69
69
  }
70
70
 
@@ -78,9 +78,7 @@
78
78
 
79
79
  alert.addAction(okAction)
80
80
 
81
-
82
-
83
- // キャンセルボタンの設定
81
+
84
82
 
85
83
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
86
84
 
@@ -88,8 +86,6 @@
88
86
 
89
87
 
90
88
 
91
- // テキストフィールドを追加
92
-
93
89
  alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
94
90
 
95
91
  textField.placeholder = "テキスト"
@@ -100,19 +96,15 @@
100
96
 
101
97
 
102
98
 
103
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
99
+ alert.view.setNeedsLayout()
104
-
105
-
106
-
107
- // アラートを画面に表示
108
100
 
109
101
  self.present(alert, animated: true, completion: nil)
110
102
 
111
103
 
112
104
 
113
- }
105
+ }
114
-
115
-
106
+
107
+
116
108
 
117
109
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
118
110
 
@@ -130,7 +122,7 @@
130
122
 
131
123
  switch indexPath.row {
132
124
 
133
- // 選択不可にしたい場合は"nil"を返す
125
+
134
126
 
135
127
  case 1:
136
128
 
@@ -138,7 +130,7 @@
138
130
 
139
131
 
140
132
 
141
- // 選択可にしたい場合は"indexPath"を返す
133
+
142
134
 
143
135
  default:
144
136
 
@@ -170,8 +162,14 @@
170
162
 
171
163
  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
172
164
 
165
+
166
+
173
167
  cell.textLabel?.text = kei[indexPath.row]
174
168
 
169
+ cell.textLabel?.textColor = UIColor.red
170
+
171
+ cell.backgroundColor = UIColor.black
172
+
175
173
 
176
174
 
177
175
  if kei.count > (indexPath.row + 1) {
@@ -180,11 +178,15 @@
180
178
 
181
179
  cell.selectionStyle = UITableViewCellSelectionStyle.none
182
180
 
181
+ cell.textLabel?.textColor = UIColor.black
182
+
183
+ cell.backgroundColor = UIColor.yellow
184
+
183
185
  } else {
184
186
 
185
187
  // セルの選択を許可
186
188
 
187
- cell.selectionStyle = UITableViewCellSelectionStyle.none
189
+ cell.selectionStyle = UITableViewCellSelectionStyle.blue
188
190
 
189
191
  }
190
192
 
@@ -196,25 +198,25 @@
196
198
 
197
199
 
198
200
 
199
- /*if kei.count > (indexPath.row + 1)使うには func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ を func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {*/
200
-
201
201
  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
202
202
 
203
203
 
204
204
 
205
+
206
+
205
-
207
+ if kei[indexPath.row] != "cell 追加 ボタン" {
206
-
208
+
207
- if kei.count > (indexPath.row + 1) {
209
+ return [indexPath.row]
208
-
210
+
209
- return nil
211
+ }else /*if (self.kei.append("cell 追加 ボタン"))*/{
210
-
211
- }else{
212
+
213
+
212
214
 
213
215
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
214
216
 
215
217
 
216
218
 
217
- // OKボタンの設定
219
+
218
220
 
219
221
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
220
222
 
@@ -222,19 +224,19 @@
222
224
 
223
225
 
224
226
 
225
- // OKを押した時入力されていたテキストを表示
227
+
226
228
 
227
229
  if let textFields = alert.textFields {
228
230
 
229
231
 
230
232
 
231
- // アラートに含まれるすべてのテキストフィールドを調べる
233
+
232
234
 
233
235
  for textField in textFields {
234
236
 
235
- self.kei.insert(textField.text!, at:0)
237
+ self.kei.insert(textField.text!, at:self.kei.count - 1)
236
-
237
- //self.kei.append(textField.text!)
238
+
239
+
238
240
 
239
241
  }
240
242
 
@@ -248,33 +250,25 @@
248
250
 
249
251
  alert.addAction(okAction)
250
252
 
253
+
254
+
255
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
256
+
257
+ alert.addAction(cancelAction)
258
+
259
+
260
+
261
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
262
+
263
+ textField.placeholder = "テキスト"
264
+
265
+ })
266
+
251
267
 
252
268
 
253
- // キャンセルボタンの設定
254
-
255
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
256
-
257
- alert.addAction(cancelAction)
258
-
259
269
 
260
270
 
261
- // テキストフィールドを追加
262
-
263
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
264
-
265
- textField.placeholder = "テキスト"
266
-
267
- })
268
-
269
-
270
-
271
-
272
-
273
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
271
+ alert.view.setNeedsLayout()
274
-
275
-
276
-
277
- // アラートを画面に表示
278
272
 
279
273
  self.present(alert, animated: true, completion: nil)
280
274
 
@@ -286,25 +280,9 @@
286
280
 
287
281
 
288
282
 
289
- //チェックマークについて
290
-
291
- /*if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{
292
-
293
- tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
294
-
295
- }else{
296
-
297
- tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
298
-
299
- }*/
300
-
301
-
302
-
303
- }
283
+ }
304
-
305
-
306
-
307
- //cellの削除について
284
+
285
+
308
286
 
309
287
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
310
288
 
@@ -342,69 +320,57 @@
342
320
 
343
321
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
344
322
 
345
-
346
-
347
- // OKボタンの設定
323
+
348
324
 
349
325
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
350
326
 
351
327
  (action:UIAlertAction!) -> Void in
352
328
 
353
-
354
-
355
- // OKを押した時入力されていたテキストを表示
329
+
356
330
 
357
331
  if let textFields = alert.textFields {
358
332
 
333
+
334
+
335
+ for textField in textFields {
336
+
337
+ self.kei[indexPath.row] = textField.text!
338
+
339
+ //self.kei.append(textField.text!)
340
+
341
+ }
342
+
343
+ self.mytableView.reloadData()
344
+
359
345
 
360
346
 
361
- // アラートに含まれるすべてのテキストフィールドを調べる
362
-
363
- for textField in textFields {
364
-
365
- self.kei[indexPath.row] = textField.text!
366
-
367
- //self.kei.append(textField.text!)
368
-
369
- }
347
+ }
348
+
370
-
349
+ })
350
+
371
- self.mytableView.reloadData()
351
+ alert.addAction(okAction)
372
352
 
373
353
 
374
354
 
355
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
356
+
375
- }
357
+ alert.addAction(cancelAction)
358
+
359
+
360
+
361
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
362
+
363
+ textField.placeholder = "テキスト"
376
364
 
377
365
  })
378
366
 
367
+
368
+
369
+
370
+
379
- alert.addAction(okAction)
371
+ alert.view.setNeedsLayout()
380
-
381
-
382
-
383
- // キャンセルボタンの設定
372
+
384
-
385
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
373
+
386
-
387
- alert.addAction(cancelAction)
388
-
389
-
390
-
391
- // テキストフィールドを追加
392
-
393
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
394
-
395
- textField.placeholder = "テキスト"
396
-
397
- })
398
-
399
-
400
-
401
-
402
-
403
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
404
-
405
-
406
-
407
- // アラートを画面に表示
408
374
 
409
375
  self.present(alert, animated: true, completion: nil)
410
376
 
@@ -422,7 +388,7 @@
422
388
 
423
389
 
424
390
 
425
- // trueを返すことでCellのアクションを許可しています
391
+
426
392
 
427
393
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
428
394
 
@@ -454,6 +420,8 @@
454
420
 
455
421
  }
456
422
 
423
+
424
+
457
425
  ```
458
426
 
459
427
 

4

イメージの追加

2017/09/29 04:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -474,7 +474,7 @@
474
474
 
475
475
 
476
476
 
477
- ![イメージ説明](0243a786355a438db9687bf367bd2d1c.png)
477
+ ![イメージ説明](36d2b852c43bc5cf9a03b9e5fa3dbec6.png)
478
478
 
479
479
 
480
480
 

3

コードエラー

2017/09/29 04:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -475,3 +475,7 @@
475
475
 
476
476
 
477
477
  ![イメージ説明](0243a786355a438db9687bf367bd2d1c.png)
478
+
479
+
480
+
481
+ ![イメージ説明](16b8b6cd19bdb9f135f44cdd58937c30.png)

2

画像の追加

2017/09/29 03:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -471,3 +471,7 @@
471
471
  前までは**+**ボタンからでもアラートテキストで入力した内容がcellに追加できたのですが色々コードを書き足している内に出来なくなりました。
472
472
 
473
473
  それと[cell 追加ボタン]がどういう仕組みでtableViewに表示されているのかが分からないので**+**ボタンの中に[cell 追加ボタン]の処理の書き方が分からなく困っています。
474
+
475
+
476
+
477
+ ![イメージ説明](0243a786355a438db9687bf367bd2d1c.png)

1

タイトルの表示

2017/09/28 23:15

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- tableView title addボタン
1
+ UITableView 表示の仕組み
test CHANGED
@@ -456,7 +456,7 @@
456
456
 
457
457
  ```
458
458
 
459
- ![イメージ説明](71b16190ef95277c217b503806ec2082.png)
459
+
460
460
 
461
461
 
462
462