回答編集履歴

1

追記しました

2018/04/30 00:06

投稿

newmt
newmt

スコア1277

test CHANGED
@@ -123,3 +123,215 @@
123
123
  }
124
124
 
125
125
  ```
126
+
127
+
128
+
129
+
130
+
131
+ [追記]
132
+
133
+
134
+
135
+ ```
136
+
137
+ import UIKit
138
+
139
+
140
+
141
+ class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
142
+
143
+
144
+
145
+ @IBOutlet weak var mytableView: UITableView!
146
+
147
+ var item = [String]()
148
+
149
+
150
+
151
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
152
+
153
+ return item.count
154
+
155
+ }
156
+
157
+
158
+
159
+ //アラート
160
+
161
+ func addalert() {
162
+
163
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
164
+
165
+
166
+
167
+ // OKボタンの設定
168
+
169
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
170
+
171
+ (action:UIAlertAction!) -> Void in
172
+
173
+
174
+
175
+ // OKを押した時入力されていたテキストを表示
176
+
177
+ if let textFields = alert.textFields {
178
+
179
+
180
+
181
+ // アラートに含まれるすべてのテキストフィールドを調べる
182
+
183
+ for textField in textFields {
184
+
185
+
186
+
187
+ self.item.insert(textField.text!, at: 0)
188
+
189
+ self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic)
190
+
191
+ print(textField.text!)
192
+
193
+ }
194
+
195
+ }
196
+
197
+ })
198
+
199
+ alert.addAction(okAction)
200
+
201
+
202
+
203
+ // キャンセルボタンの設定
204
+
205
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
206
+
207
+ alert.addAction(cancelAction)
208
+
209
+
210
+
211
+ // テキストフィールドを追加
212
+
213
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
214
+
215
+ textField.placeholder = "テキスト"
216
+
217
+ })
218
+
219
+
220
+
221
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
222
+
223
+
224
+
225
+ // アラートを画面に表示
226
+
227
+ self.present(alert, animated: true, completion: nil)
228
+
229
+ }
230
+
231
+
232
+
233
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
234
+
235
+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
236
+
237
+ let todoLabel = item[indexPath.row]
238
+
239
+ cell.textLabel?.text = todoLabel
240
+
241
+
242
+
243
+ //ボタンについて
244
+
245
+ let button = UIButton()
246
+
247
+ button.backgroundColor = UIColor.blue
248
+
249
+ button.setTitle(" 追 加 ", for: .normal)
250
+
251
+ cell.contentView.addSubview(button)
252
+
253
+
254
+
255
+ button.translatesAutoresizingMaskIntoConstraints = false
256
+
257
+ cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true
258
+
259
+ // 中央にする
260
+
261
+ //button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
262
+
263
+ //丸みに対して
264
+
265
+ button.layer.cornerRadius = 10
266
+
267
+ button.layer.masksToBounds = true
268
+
269
+
270
+
271
+ cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true
272
+
273
+
274
+
275
+ //ラベルについて
276
+
277
+ let label = UILabel()
278
+
279
+ label.backgroundColor = UIColor.darkGray
280
+
281
+
282
+
283
+ if indexPath.row < item.count {
284
+
285
+ label.text = item[indexPath.row]
286
+
287
+ }
288
+
289
+
290
+
291
+ //丸みに対して
292
+
293
+ label.layer.cornerRadius = 5
294
+
295
+ label.layer.masksToBounds = true
296
+
297
+ cell.contentView.addSubview(label)
298
+
299
+
300
+
301
+ label.translatesAutoresizingMaskIntoConstraints = false
302
+
303
+ cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true
304
+
305
+ cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true
306
+
307
+
308
+
309
+ return cell
310
+
311
+ }
312
+
313
+
314
+
315
+ @IBAction func addlabel(_ sender: Any) {
316
+
317
+ addalert()
318
+
319
+ }
320
+
321
+
322
+
323
+
324
+
325
+ override func viewDidLoad() {
326
+
327
+ super.viewDidLoad()
328
+
329
+ mytableView.dataSource = self
330
+
331
+ mytableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
332
+
333
+ }
334
+
335
+ }
336
+
337
+ ```