teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

追記

2018/05/02 03:28

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes

4

追記しました

2018/05/02 03:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -249,4 +249,5 @@
249
249
  self.present(alert, animated: true, completion: nil)
250
250
 
251
251
  }
252
- ```
252
+ ```
253
+ alertで入力し挿入するタイミングでsignalsigabrtになりました。

3

追記

2018/05/02 01:13

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -194,4 +194,59 @@
194
194
  label4.text = add[indexPath.row]
195
195
  label5.text = add[indexPath.row]
196
196
  }
197
+
198
+
199
+ func alert(){
200
+ // テキストフィールド付きアラート表示
201
+
202
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
203
+
204
+ // OKボタンの設定
205
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
206
+ (action:UIAlertAction!) -> Void in
207
+
208
+ // OKを押した時入力されていたテキストを表示
209
+ if let textFields = alert.textFields {
210
+
211
+ // アラートに含まれるすべてのテキストフィールドを調べる
212
+ for textField in textFields {
213
+ if self.cnt < 1{
214
+ self.item1.insert(textField.text!, at: 0)
215
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
216
+ }else if self.cnt < 2{
217
+ self.item2.insert(textField.text!, at: 0)
218
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
219
+ }else if self.cnt < 3{
220
+ self.item3.insert(textField.text!, at: 0)
221
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
222
+ }else if self.cnt < 4{
223
+ self.item4.insert(textField.text!, at: 0)
224
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
225
+ }else if self.cnt < 5{
226
+ self.item5.insert(textField.text!, at: 0)
227
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
228
+ }
229
+ //print(textField.text!)
230
+ //self.Gesture()
231
+ //self.doubleclic()
232
+ }
233
+ }
234
+ })
235
+ alert.addAction(okAction)
236
+
237
+ // キャンセルボタンの設定
238
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
239
+ alert.addAction(cancelAction)
240
+
241
+ // テキストフィールドを追加
242
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
243
+ textField.placeholder = "テキスト"
244
+ })
245
+
246
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
247
+
248
+ // アラートを画面に表示
249
+ self.present(alert, animated: true, completion: nil)
250
+
251
+ }
197
252
  ```

2

コードの追記

2018/05/02 01:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -113,16 +113,85 @@
113
113
  }
114
114
  ```
115
115
  ### やりたい事
116
- 現状ではlabelcellに挿入するときに一行に一個という形でlabelが挿入される形になっているがそれを一つのセルの中に3つだけlabelを挿入できるようにしたい
116
+ alert使って一個ずつlabelを挿入できるようにしたい
117
- 例)
118
-
119
- 1
120
- 2
121
- 3
122
- 4
123
- やりたい事
124
- 1 2 3 4
125
117
 
118
+
126
119
  **出来ている事**
120
+ 横一列にセルを挿入出来た。
121
+
122
+ **困っていること**
123
+ alert一回で五つ同じlabelが挿入されてしまう。
127
- 挿入できる数は制限が出来たが,のよにすれば横列に挿入できるのかが分からない。
124
+ どうしたら個ずつlabelを挿入できるのかが分からなくて困ってます
125
+
126
+ ```swift
127
+ //labelインスタンス
128
+ //label1
129
+ let label = UILabel()
130
+ label.backgroundColor = UIColor.darkGray
131
+ //label2
132
+ let label2 = UILabel()
133
+ label2.backgroundColor = UIColor.darkGray
134
+ //label3
135
+ let label3 = UILabel()
136
+ label3.backgroundColor = UIColor.darkGray
137
+ //label4
138
+ let label4 = UILabel()
139
+ label4.backgroundColor = UIColor.darkGray
140
+ //label5
141
+ let label5 = UILabel()
142
+ label5.backgroundColor = UIColor.darkGray
143
+
144
+ //丸みに対して
145
+ //label1
146
+ label.layer.cornerRadius = 5
147
+ label.layer.masksToBounds = true
148
+ label.sizeToFit()
128
- 個人的には配列は使うと出来るのではないかと思っているがどのようにコードを書けばいいか分からなくて困っています。
149
+ label.translatesAutoresizingMaskIntoConstraints = false
150
+ cell.contentView.addSubview(label)
151
+ //label2
152
+ label2.layer.cornerRadius = 5
153
+ label2.layer.masksToBounds = true
154
+ label2.sizeToFit()
155
+ label2.translatesAutoresizingMaskIntoConstraints = false
156
+ cell.contentView.addSubview(label2)
157
+ //label3
158
+ label3.layer.cornerRadius = 5
159
+ label3.layer.masksToBounds = true
160
+ label3.sizeToFit()
161
+ label3.translatesAutoresizingMaskIntoConstraints = false
162
+ cell.contentView.addSubview(label3)
163
+ //label4
164
+ label4.layer.cornerRadius = 5
165
+ label4.layer.masksToBounds = true
166
+ label4.sizeToFit()
167
+ label4.translatesAutoresizingMaskIntoConstraints = false
168
+ cell.contentView.addSubview(label4)
169
+ //label5
170
+ label5.layer.cornerRadius = 5
171
+ label5.layer.masksToBounds = true
172
+ label5.sizeToFit()
173
+ label5.translatesAutoresizingMaskIntoConstraints = false
174
+ cell.contentView.addSubview(label5)
175
+
176
+ //座標的な
177
+ cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true
178
+ cell.contentView.leftAnchor.constraint(equalTo: label2.leftAnchor, constant: -70).isActive = true
179
+ cell.contentView.leftAnchor.constraint(equalTo: label3.leftAnchor, constant: -134).isActive = true
180
+ cell.contentView.leftAnchor.constraint(equalTo: label4.leftAnchor, constant: -198).isActive = true
181
+ cell.contentView.leftAnchor.constraint(equalTo: label5.leftAnchor, constant: -262).isActive = true
182
+
183
+ cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true
184
+ cell.contentView.heightAnchor.constraint(equalTo: label2.heightAnchor, multiplier: 1).isActive = true
185
+ cell.contentView.heightAnchor.constraint(equalTo: label3.heightAnchor, multiplier: 1).isActive = true
186
+ cell.contentView.heightAnchor.constraint(equalTo: label4.heightAnchor, multiplier: 1).isActive = true
187
+ cell.contentView.heightAnchor.constraint(equalTo: label5.heightAnchor, multiplier: 1).isActive = true
188
+
189
+ // itemに格納した値を設定
190
+ if indexPath.row < add.count {
191
+ label.text = add[indexPath.row]
192
+ label2.text = add[indexPath.row]
193
+ label3.text = add[indexPath.row]
194
+ label4.text = add[indexPath.row]
195
+ label5.text = add[indexPath.row]
196
+ }
197
+ ```

1

誤字

2018/05/02 00:51

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -91,12 +91,10 @@
91
91
  }
92
92
 
93
93
  @IBAction func addlabel(_ sender: Any) {
94
- var key = 0
94
+
95
- if key < 5 {
96
95
  alert()
97
- }
98
- key += 1
99
96
 
97
+
100
98
  }
101
99
 
102
100
 
@@ -115,7 +113,7 @@
115
113
  }
116
114
  ```
117
115
  ### やりたい事
118
- 現状ではlabelをcellに挿入するときに一行に一個という形でlabelが挿入される形になっているがそれを一つのセルの中に複数のlabelを入れれるようにしたい
116
+ 現状ではlabelをcellに挿入するときに一行に一個という形でlabelが挿入される形になっているがそれを一つのセルの中に3つだけlabelをできるようにしたい
119
117
  例)
120
118
 
121
119
  1
@@ -126,5 +124,5 @@
126
124
  1 2 3 4
127
125
 
128
126
  **出来ている事**
129
- 挿入できる数制限したいのでそれに関しては出来たが,どのようにすれば横一列に挿入できるのかが分からない。
127
+ 挿入できる数制限出来たが,どのようにすれば横一列に挿入できるのかが分からない。
130
128
  個人的には配列は使うと出来るのではないかと思っているがどのようにコードを書けばいいか分からなくて困っています。