質問編集履歴

1

コードの追加

2017/03/10 09:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,6 +18,464 @@
18
18
 
19
19
 
20
20
 
21
+ //編集・追加
22
+
23
+ ```swift
24
+
25
+ import UIKit
26
+
27
+
28
+
29
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
30
+
31
+
32
+
33
+
34
+
35
+ @IBOutlet weak var tableView: UITableView!
36
+
37
+
38
+
39
+ var sectionTitleArray = [String]()
40
+
41
+ var dataArray1 = ["add Row"]
42
+
43
+ var dataArrayGroup: [[DataModel]] = []
44
+
45
+
46
+
47
+
48
+
49
+ override func viewDidLoad() {
50
+
51
+ super.viewDidLoad()
52
+
53
+
54
+
55
+ tableView.estimatedRowHeight = 44
56
+
57
+ tableView.rowHeight = UITableViewAutomaticDimension
58
+
59
+ tableView.tableFooterView = UIView()
60
+
61
+ }
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ @IBAction func addSection(_ sender: UIButton) {
70
+
71
+
72
+
73
+ let alert = UIAlertController(title:"Sction追加",
74
+
75
+ message: "SectionTitleを入力してください",
76
+
77
+ preferredStyle: .alert)
78
+
79
+
80
+
81
+ let cancelAction = UIAlertAction(title: "Cancel",
82
+
83
+ style: .cancel,
84
+
85
+ handler:
86
+
87
+ { action -> Void in
88
+
89
+ print("Cancel")
90
+
91
+ })
92
+
93
+
94
+
95
+ let defaultAction = UIAlertAction(title: "OK",
96
+
97
+ style: .default,
98
+
99
+ handler:
100
+
101
+ { action -> Void in
102
+
103
+
104
+
105
+ // TextFieldから値を取得
106
+
107
+ if let textFields = alert.textFields {
108
+
109
+ for textField in textFields {
110
+
111
+
112
+
113
+ if let text = textField.text, !text.isEmpty {
114
+
115
+
116
+
117
+ // 取得したテキストをセクションのタイトルとして追加する
118
+
119
+ print(text)
120
+
121
+
122
+
123
+ self.sectionTitleArray.insert(text, at: 0)
124
+
125
+ self.dataArrayGroup.insert([], at: 0)
126
+
127
+ self.tableView.insertSections(IndexSet(integer: 0), with: .automatic)
128
+
129
+ }
130
+
131
+ }
132
+
133
+ }
134
+
135
+ })
136
+
137
+
138
+
139
+ alert.addAction(cancelAction)
140
+
141
+ alert.addAction(defaultAction)
142
+
143
+ alert.addTextField(configurationHandler: { text -> Void in })
144
+
145
+ present(alert, animated: true, completion: nil)
146
+
147
+
148
+
149
+ //addrowボタン
150
+
151
+ let count = dataArrayGroup[1].count
152
+
153
+
154
+
155
+ dataArray1[1].insert([], at: 0)
156
+
157
+ tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic)
158
+
159
+ }
160
+
161
+
162
+
163
+ @IBAction func deleteSection(_ sender: UIButton) {
164
+
165
+
166
+
167
+ if sectionTitleArray.isEmpty {
168
+
169
+ return
170
+
171
+ }
172
+
173
+
174
+
175
+ let alert = UIAlertController(title:"Sction削除",
176
+
177
+ message: "削除するSectionTitleを入力してください",
178
+
179
+ preferredStyle: .alert)
180
+
181
+
182
+
183
+ let cancelAction = UIAlertAction(title: "Cancel",
184
+
185
+ style: .cancel,
186
+
187
+ handler:
188
+
189
+ { action -> Void in
190
+
191
+ print("Cancel")
192
+
193
+ })
194
+
195
+
196
+
197
+ let defaultAction = UIAlertAction(title: "OK",
198
+
199
+ style: .default,
200
+
201
+ handler:
202
+
203
+ { action -> Void in
204
+
205
+
206
+
207
+ // TextFieldから値を取得
208
+
209
+ if let textFields = alert.textFields {
210
+
211
+ for textField in textFields {
212
+
213
+
214
+
215
+ if let text = textField.text,
216
+
217
+ !text.isEmpty,
218
+
219
+ let index = self.sectionTitleArray.index(of: text) {
220
+
221
+
222
+
223
+ self.sectionTitleArray.remove(at: index)
224
+
225
+ self.dataArrayGroup.remove(at: index)
226
+
227
+ self.tableView.deleteSections(IndexSet(integer: index), with: .automatic)
228
+
229
+ }
230
+
231
+ }
232
+
233
+ }
234
+
235
+ })
236
+
237
+
238
+
239
+ alert.addAction(cancelAction)
240
+
241
+ alert.addAction(defaultAction)
242
+
243
+ alert.addTextField(configurationHandler: { text -> Void in })
244
+
245
+ present(alert, animated: true, completion: nil)
246
+
247
+
248
+
249
+
250
+
251
+ }
252
+
253
+
254
+
255
+ // Rowを追加する(アニメーションはご自由に)
256
+
257
+ @IBAction func addRow(_ sender: UIButton) {
258
+
259
+ //func addRow() {
260
+
261
+
262
+
263
+ if dataArrayGroup.count == 0 {
264
+
265
+ return
266
+
267
+ }
268
+
269
+
270
+
271
+ let alert = UIAlertController(title:"Row追加",
272
+
273
+ message: "個数と金額を入力してください",
274
+
275
+ preferredStyle: .alert)
276
+
277
+
278
+
279
+ let cancelAction = UIAlertAction(title: "Cancel",
280
+
281
+ style: .cancel,
282
+
283
+ handler:
284
+
285
+ { action -> Void in
286
+
287
+ print("Cancel")
288
+
289
+ })
290
+
291
+
292
+
293
+ let count = dataArrayGroup[0].count
294
+
295
+ let defaultAction = UIAlertAction(title: "OK",
296
+
297
+ style: .default,
298
+
299
+ handler:
300
+
301
+ { action -> Void in
302
+
303
+
304
+
305
+ // TextFieldから値を取得
306
+
307
+ if let textFields = alert.textFields {
308
+
309
+
310
+
311
+ let data = DataModel()
312
+
313
+
314
+
315
+ for textField in textFields {
316
+
317
+
318
+
319
+ if let text = textField.text, !text.isEmpty, let _ = Int(text) {
320
+
321
+ if textField.tag == 1 {
322
+
323
+ data.count = text
324
+
325
+ } else {
326
+
327
+ data.price = text
328
+
329
+ }
330
+
331
+ }
332
+
333
+ }
334
+
335
+
336
+
337
+ self.dataArrayGroup[0].insert(data, at: count)
338
+
339
+ self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic)
340
+
341
+ }
342
+
343
+ })
344
+
345
+
346
+
347
+
348
+
349
+ alert.addTextField {
350
+
351
+ $0.placeholder = "個数"
352
+
353
+ $0.keyboardType = .numberPad
354
+
355
+ $0.tag = 1
356
+
357
+ }
358
+
359
+ alert.addTextField {
360
+
361
+ $0.placeholder = "金額"
362
+
363
+ $0.keyboardType = .numberPad
364
+
365
+ $0.tag = 2
366
+
367
+ }
368
+
369
+
370
+
371
+ alert.addAction(cancelAction)
372
+
373
+ alert.addAction(defaultAction)
374
+
375
+ present(alert, animated: true, completion: nil)
376
+
377
+ }
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+ // MARK: - TableView Delegate & DataSource
386
+
387
+ //この部分です。
388
+
389
+ func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
390
+
391
+ if sectionTitleArray.count == 0 {
392
+
393
+ return nil
394
+
395
+ } else {
396
+
397
+ return sectionTitleArray[section]
398
+
399
+ }
400
+
401
+ }
402
+
403
+
404
+
405
+ // Section Count
406
+
407
+ func numberOfSections(in tableView: UITableView) -> Int {
408
+
409
+ return dataArrayGroup.count
410
+
411
+ }
412
+
413
+
414
+
415
+ // Row Count
416
+
417
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
418
+
419
+ return dataArrayGroup[section].count
420
+
421
+ }
422
+
423
+
424
+
425
+ // Generate Cell
426
+
427
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
428
+
429
+ let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
430
+
431
+ cell.data = dataArrayGroup[indexPath.section][indexPath.row]
432
+
433
+ cell.indexLabel.text = String(indexPath.row + 1)
434
+
435
+ return cell
436
+
437
+ }
438
+
439
+
440
+
441
+ // Select Cell
442
+
443
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
444
+
445
+ tableView.deselectRow(at: indexPath as IndexPath, animated: true)
446
+
447
+ }
448
+
449
+
450
+
451
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
452
+
453
+ if editingStyle == UITableViewCellEditingStyle.delete {
454
+
455
+ dataArrayGroup[indexPath.section].remove(at: indexPath.row)
456
+
457
+ tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
458
+
459
+ }
460
+
461
+ }
462
+
463
+
464
+
465
+ //cellが削除が可能なことを伝える
466
+
467
+ func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
468
+
469
+ return UITableViewCellEditingStyle.delete;
470
+
471
+ }
472
+
473
+ }
474
+
475
+ ```
476
+
477
+
478
+
21
479
  参考文献
22
480
 
23
481
  KentarouさんのGitHub