質問編集履歴

1

コードの変更

2018/04/03 05:39

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,12 +14,16 @@
14
14
 
15
15
  var add = UILabel()
16
16
 
17
- let item = ["kei"]
17
+ var item = [String]()
18
+
19
+
20
+
21
+
22
+
23
+ //+ボタン
18
24
 
19
25
  @IBAction func addlabel(_ sender: Any) {
20
26
 
21
-
22
-
23
27
  alert()
24
28
 
25
29
  }
@@ -30,376 +34,392 @@
30
34
 
31
35
  func label(){
32
36
 
37
+ add = UILabel(frame: CGRect(x: 130, y:250, width: 100, height:20))
38
+
39
+ //ラベルの大きさ、座標指定
40
+
41
+ //add.text = "labelです"
42
+
43
+ //文字を変更
44
+
45
+
46
+
47
+ add.backgroundColor = UIColor.lightGray
48
+
49
+
50
+
51
+ add.font = UIFont.systemFont(ofSize: 30)
52
+
53
+ //文字の大きさ
54
+
55
+
56
+
57
+ add.textColor = UIColor.black
58
+
59
+ //文字カラー
60
+
61
+
62
+
63
+ add.sizeToFit()
64
+
65
+ //文字数にあわせてlabelの大きさを変更(サイズが文字にフィットする)
66
+
67
+
68
+
69
+ self.view.addSubview(add)
70
+
71
+ //実際にviewに見える形でlabelが出現する
72
+
73
+ }
74
+
75
+
76
+
77
+ func alert(){
78
+
79
+ // テキストフィールド付きアラート表示
80
+
81
+
82
+
83
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
84
+
85
+
86
+
87
+ // OKボタンの設定
88
+
89
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
90
+
91
+ (action:UIAlertAction!) -> Void in
92
+
93
+
94
+
95
+ // OKを押した時入力されていたテキストを表示
96
+
97
+ if let textFields = alert.textFields {
98
+
99
+
100
+
101
+ // アラートに含まれるすべてのテキストフィールドを調べる
102
+
103
+ for textField in textFields {
104
+
105
+ self.label()
106
+
107
+ self.item.insert(textField.text!, at: 0)
108
+
109
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
110
+
111
+ self.add.text = textField.text!
112
+
113
+ self.add.sizeToFit()
114
+
115
+ print(textField.text!)
116
+
117
+ self.Gesture()
118
+
119
+ self.doubleclic()
120
+
121
+ }
122
+
123
+ }
124
+
125
+ })
126
+
127
+ alert.addAction(okAction)
128
+
129
+
130
+
131
+ // キャンセルボタンの設定
132
+
133
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
134
+
135
+ alert.addAction(cancelAction)
136
+
137
+
138
+
139
+ // テキストフィールドを追加
140
+
141
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
142
+
143
+ textField.placeholder = "テキスト"
144
+
145
+ })
146
+
147
+
148
+
149
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
150
+
151
+
152
+
153
+ // アラートを画面に表示
154
+
155
+ self.present(alert, animated: true, completion: nil)
156
+
157
+
158
+
159
+ }
160
+
161
+
162
+
163
+ func deletealert() {
164
+
165
+ let alert = UIAlertController(title:"you realy want to delete?", message: "メッセージ", preferredStyle: .alert)
166
+
167
+
168
+
169
+ let okAction = UIAlertAction(title: "YES", style: .default, handler: {
170
+
171
+ (action:UIAlertAction!) -> Void in
172
+
173
+
174
+
175
+ //labelの削除について
176
+
177
+ self.add.tag = 1
178
+
179
+
180
+
181
+ self.view.subviews.forEach {
182
+
183
+ if $0.tag == 1{
184
+
185
+ $0.removeFromSuperview()
186
+
187
+
188
+
189
+ }
190
+
191
+ }
192
+
193
+ })
194
+
195
+ alert.addAction(okAction)
196
+
197
+
198
+
199
+ // キャンセルボタンの設定
200
+
201
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
202
+
203
+ alert.addAction(cancelAction)
204
+
205
+
206
+
207
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
208
+
209
+ // アラートを画面に表示
210
+
211
+ self.present(alert, animated: true, completion: nil)
212
+
213
+
214
+
215
+ }
216
+
217
+
218
+
219
+
220
+
221
+ @objc func longpress(sender: UILongPressGestureRecognizer){
222
+
223
+
224
+
225
+ // 長押し開始〜
226
+
227
+ if(sender.state == UIGestureRecognizerState.began)
228
+
229
+ {
230
+
231
+
232
+
233
+ } else if (sender.state == UIGestureRecognizerState.ended)
234
+
235
+ {
236
+
237
+ //labelの削除について
238
+
239
+ self.add.tag = 1
240
+
241
+
242
+
243
+ self.view.subviews.forEach {
244
+
245
+ if $0.tag == 1{
246
+
247
+ $0.removeFromSuperview()
248
+
249
+
250
+
251
+ }
252
+
253
+ }
254
+
255
+ alert()
256
+
257
+ print("ロングタップされたよ。")
258
+
259
+ }
260
+
261
+
262
+
263
+ }
264
+
265
+
266
+
267
+ func Gesture() {
268
+
269
+
270
+
271
+ // UILongPressGestureRecognizerインスタンス作成
272
+
273
+ let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(sender:)))
274
+
275
+ // 時間(デフォルト0.5秒)
276
+
277
+ longPressGesture.minimumPressDuration = 0.5
278
+
279
+ self.add.isUserInteractionEnabled = true
280
+
281
+ self.add.addGestureRecognizer(longPressGesture)
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+ }
290
+
291
+
292
+
293
+ func doubleclic(){
294
+
295
+ // ダブルタップ
296
+
297
+ let doubeltapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubletap(sender:)))
298
+
299
+ doubeltapGesture.numberOfTapsRequired = 2
300
+
301
+ self.add.isUserInteractionEnabled = true
302
+
303
+ self.add.addGestureRecognizer(doubeltapGesture)
304
+
305
+ }
306
+
307
+
308
+
309
+ @objc func doubletap(sender: UITapGestureRecognizer){
310
+
311
+ if(sender.state == UIGestureRecognizerState.began)
312
+
313
+ {
314
+
315
+ }else if(sender.state == UIGestureRecognizerState.ended)
316
+
317
+ {
318
+
319
+ deletealert()
320
+
321
+ print("tapされたよ")
322
+
323
+ }
324
+
325
+
326
+
327
+ }
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
336
+
337
+ return item.count
338
+
339
+ }
340
+
341
+
342
+
343
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
344
+
345
+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
346
+
347
+ //cell.textLabel?.text = add.text
348
+
349
+ let todoLabel = item[indexPath.row]
350
+
351
+ cell.textLabel?.text = todoLabel
352
+
353
+ return cell
354
+
355
+ }
356
+
357
+
358
+
359
+
360
+
361
+ override func viewDidLoad() {
362
+
363
+ super.viewDidLoad()
364
+
365
+ mytableView.dataSource = self
366
+
367
+ mytableView.delegate = self
368
+
369
+ }
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+ override func didReceiveMemoryWarning() {
378
+
379
+ super.didReceiveMemoryWarning()
380
+
381
+ // Dispose of any resources that can be recreated.
382
+
383
+ }
384
+
385
+
386
+
387
+
388
+
389
+ }
390
+
391
+
392
+
393
+
394
+
395
+ ```
396
+
397
+
398
+
399
+ **やりたいこと**
400
+
401
+ tabelviewのcell内にlabelを挿入したい
402
+
403
+
404
+
405
+ **困っていること**
406
+
407
+ labelをコードから作っているためcell内に挿入する方法が分からない
408
+
409
+ エラーは起きないがcellの中に挿入できずにいる。コンソールには入力した文字は表示されるので下記のコードが良くないのかなと推測できるがどうすればいいの分からずいに困っています。
410
+
411
+ //ラベルについて
412
+
413
+ ```ここに言語を入力
414
+
415
+ func label(){
416
+
33
417
  add = UILabel (frame: CGRect())
34
418
 
35
- //ラベルの大きさ、座標指定
36
-
37
- //add.text = "labelです"
38
-
39
- //文字を変更
40
-
41
-
42
-
43
- add.backgroundColor = UIColor.lightGray
44
-
45
-
46
-
47
- add.font = UIFont.systemFont(ofSize: 30)
48
-
49
- //文字の大きさ
50
-
51
-
52
-
53
- add.textColor = UIColor.black
54
-
55
- //文字カラー
56
-
57
-
58
-
59
- add.sizeToFit()
60
-
61
- //文字数にあわせてlabelの大きさを変更(サイズが文字にフィットする)
62
-
63
-
64
-
65
- self.view.addSubview(add)
66
-
67
- //実際にviewに見える形でlabelが出現する
68
-
69
- }
70
-
71
-
72
-
73
- func alert(){
74
-
75
- // テキストフィールド付きアラート表示
76
-
77
-
78
-
79
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
80
-
81
-
82
-
83
- // OKボタンの設定
84
-
85
- let okAction = UIAlertAction(title: "OK", style: .default, handler: {
86
-
87
- (action:UIAlertAction!) -> Void in
88
-
89
-
90
-
91
- // OKを押した時入力されていたテキストを表示
92
-
93
- if let textFields = alert.textFields {
94
-
95
-
96
-
97
- // アラートに含まれるすべてのテキストフィールドを調べる
98
-
99
- for textField in textFields {
100
-
101
- self.label()
102
-
103
- self.add.text = textField.text!
104
-
105
- self.add.sizeToFit()
106
-
107
- print(textField.text!)
108
-
109
- self.Gesture()
110
-
111
- self.doubleclic()
112
-
113
- }
114
-
115
- }
116
-
117
- })
419
+ ...
118
-
119
- alert.addAction(okAction)
420
+
120
-
121
-
122
-
123
- // キャンセルボタンの設定
124
-
125
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
126
-
127
- alert.addAction(cancelAction)
128
-
129
-
130
-
131
- // テキストフィールドを追加
132
-
133
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
134
-
135
- textField.placeholder = "テキスト"
136
-
137
- })
421
+ ...
138
-
139
-
140
-
141
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
142
-
143
-
144
-
145
- // アラートを画面に表示
146
-
147
- self.present(alert, animated: true, completion: nil)
148
-
149
-
150
-
151
- }
152
-
153
-
154
-
155
- func deletealert() {
156
-
157
- let alert = UIAlertController(title:"you realy want to delete?", message: "メッセージ", preferredStyle: .alert)
158
-
159
-
160
-
161
- let okAction = UIAlertAction(title: "YES", style: .default, handler: {
162
-
163
- (action:UIAlertAction!) -> Void in
164
-
165
-
166
-
167
- //labelの削除について
168
-
169
- self.add.tag = 1
170
-
171
-
172
-
173
- self.view.subviews.forEach {
174
-
175
- if $0.tag == 1{
176
-
177
- $0.removeFromSuperview()
178
-
179
-
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
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
200
-
201
- // アラートを画面に表示
202
-
203
- self.present(alert, animated: true, completion: nil)
204
-
205
-
206
-
207
- }
208
-
209
-
210
-
211
-
212
-
213
- @objc func longpress(sender: UILongPressGestureRecognizer){
214
-
215
-
216
-
217
- // 長押し開始〜
218
-
219
- if(sender.state == UIGestureRecognizerState.began)
220
-
221
- {
222
-
223
-
224
-
225
- } else if (sender.state == UIGestureRecognizerState.ended)
226
-
227
- {
228
-
229
- //labelの削除について
230
-
231
- self.add.tag = 1
232
-
233
-
234
-
235
- self.view.subviews.forEach {
236
-
237
- if $0.tag == 1{
238
-
239
- $0.removeFromSuperview()
240
-
241
-
242
-
243
- }
244
-
245
- }
246
-
247
- alert()
248
-
249
- print("ロングタップされたよ。")
250
-
251
- }
252
-
253
-
254
-
255
- }
256
-
257
-
258
-
259
- func Gesture() {
260
-
261
-
262
-
263
- // UILongPressGestureRecognizerインスタンス作成
264
-
265
- let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(sender:)))
266
-
267
- // 時間(デフォルト0.5秒)
268
-
269
- longPressGesture.minimumPressDuration = 0.5
270
-
271
- self.add.isUserInteractionEnabled = true
272
-
273
- self.add.addGestureRecognizer(longPressGesture)
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
- }
282
-
283
-
284
-
285
- func doubleclic(){
286
-
287
- // ダブルタップ
288
-
289
- let doubeltapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubletap(sender:)))
290
-
291
- doubeltapGesture.numberOfTapsRequired = 2
292
-
293
- self.add.isUserInteractionEnabled = true
294
-
295
- self.add.addGestureRecognizer(doubeltapGesture)
296
-
297
- }
298
-
299
-
300
-
301
- @objc func doubletap(sender: UITapGestureRecognizer){
302
-
303
- if(sender.state == UIGestureRecognizerState.began)
304
-
305
- {
306
-
307
- }else if(sender.state == UIGestureRecognizerState.ended)
308
-
309
- {
310
-
311
- deletealert()
312
-
313
- print("tapされたよ")
314
-
315
- }
316
-
317
-
318
-
319
- }
320
-
321
-
322
-
323
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
324
-
325
- return item.count
326
-
327
- }
328
-
329
-
330
-
331
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
332
-
333
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
334
-
335
- cell.textLabel?.text = add.text
336
-
337
- return cell
338
-
339
- }
340
-
341
-
342
-
343
-
344
-
345
- override func viewDidLoad() {
346
-
347
- super.viewDidLoad()
348
-
349
-
350
-
351
- }
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
- override func didReceiveMemoryWarning() {
360
-
361
- super.didReceiveMemoryWarning()
362
-
363
- // Dispose of any resources that can be recreated.
364
-
365
- }
366
-
367
-
368
-
369
-
370
422
 
371
423
  }
372
424
 
373
-
374
-
375
425
  ```
376
-
377
-
378
-
379
- **やりたいこと**
380
-
381
- tabelviewのcell内にlabelを挿入したい
382
-
383
-
384
-
385
- **困っていること**
386
-
387
- labelをコードから作っているためcell内に挿入する方法が分からない
388
-
389
- エラーは起きないがcellの中に挿入できずにいる。コンソールには入力した文字は表示されるので下記のコードが良くないのかなと推測できるがどうすればいいの分からずいに困っています。
390
-
391
- //ラベルについて
392
-
393
- ```ここに言語を入力
394
-
395
- func label(){
396
-
397
- add = UILabel (frame: CGRect())
398
-
399
- ...
400
-
401
- ...
402
-
403
- }
404
-
405
- ```