質問編集履歴
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,5 +8,234 @@
|
|
8
8
|
**試してみたこと**
|
9
9
|
自分ではとあるgithubを参考にしてsectionを追加するボタンと削除できるボタンを作ったのですがrowを追加するボタンの作り方が分からなくて色々試してみたのですがどうしても思いつかないので教えていただけたら嬉しいです。
|
10
10
|
|
11
|
+
//編集・追加
|
12
|
+
```swift
|
13
|
+
import UIKit
|
14
|
+
|
15
|
+
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
16
|
+
|
17
|
+
|
18
|
+
@IBOutlet weak var tableView: UITableView!
|
19
|
+
|
20
|
+
var sectionTitleArray = [String]()
|
21
|
+
var dataArray1 = ["add Row"]
|
22
|
+
var dataArrayGroup: [[DataModel]] = []
|
23
|
+
|
24
|
+
|
25
|
+
override func viewDidLoad() {
|
26
|
+
super.viewDidLoad()
|
27
|
+
|
28
|
+
tableView.estimatedRowHeight = 44
|
29
|
+
tableView.rowHeight = UITableViewAutomaticDimension
|
30
|
+
tableView.tableFooterView = UIView()
|
31
|
+
}
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
@IBAction func addSection(_ sender: UIButton) {
|
36
|
+
|
37
|
+
let alert = UIAlertController(title:"Sction追加",
|
38
|
+
message: "SectionTitleを入力してください",
|
39
|
+
preferredStyle: .alert)
|
40
|
+
|
41
|
+
let cancelAction = UIAlertAction(title: "Cancel",
|
42
|
+
style: .cancel,
|
43
|
+
handler:
|
44
|
+
{ action -> Void in
|
45
|
+
print("Cancel")
|
46
|
+
})
|
47
|
+
|
48
|
+
let defaultAction = UIAlertAction(title: "OK",
|
49
|
+
style: .default,
|
50
|
+
handler:
|
51
|
+
{ action -> Void in
|
52
|
+
|
53
|
+
// TextFieldから値を取得
|
54
|
+
if let textFields = alert.textFields {
|
55
|
+
for textField in textFields {
|
56
|
+
|
57
|
+
if let text = textField.text, !text.isEmpty {
|
58
|
+
|
59
|
+
// 取得したテキストをセクションのタイトルとして追加する
|
60
|
+
print(text)
|
61
|
+
|
62
|
+
self.sectionTitleArray.insert(text, at: 0)
|
63
|
+
self.dataArrayGroup.insert([], at: 0)
|
64
|
+
self.tableView.insertSections(IndexSet(integer: 0), with: .automatic)
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
})
|
69
|
+
|
70
|
+
alert.addAction(cancelAction)
|
71
|
+
alert.addAction(defaultAction)
|
72
|
+
alert.addTextField(configurationHandler: { text -> Void in })
|
73
|
+
present(alert, animated: true, completion: nil)
|
74
|
+
|
75
|
+
//addrowボタン
|
76
|
+
let count = dataArrayGroup[1].count
|
77
|
+
|
78
|
+
dataArray1[1].insert([], at: 0)
|
79
|
+
tableView.insertRows(at: [IndexPath(row: count, section: 1)], with: .automatic)
|
80
|
+
}
|
81
|
+
|
82
|
+
@IBAction func deleteSection(_ sender: UIButton) {
|
83
|
+
|
84
|
+
if sectionTitleArray.isEmpty {
|
85
|
+
return
|
86
|
+
}
|
87
|
+
|
88
|
+
let alert = UIAlertController(title:"Sction削除",
|
89
|
+
message: "削除するSectionTitleを入力してください",
|
90
|
+
preferredStyle: .alert)
|
91
|
+
|
92
|
+
let cancelAction = UIAlertAction(title: "Cancel",
|
93
|
+
style: .cancel,
|
94
|
+
handler:
|
95
|
+
{ action -> Void in
|
96
|
+
print("Cancel")
|
97
|
+
})
|
98
|
+
|
99
|
+
let defaultAction = UIAlertAction(title: "OK",
|
100
|
+
style: .default,
|
101
|
+
handler:
|
102
|
+
{ action -> Void in
|
103
|
+
|
104
|
+
// TextFieldから値を取得
|
105
|
+
if let textFields = alert.textFields {
|
106
|
+
for textField in textFields {
|
107
|
+
|
108
|
+
if let text = textField.text,
|
109
|
+
!text.isEmpty,
|
110
|
+
let index = self.sectionTitleArray.index(of: text) {
|
111
|
+
|
112
|
+
self.sectionTitleArray.remove(at: index)
|
113
|
+
self.dataArrayGroup.remove(at: index)
|
114
|
+
self.tableView.deleteSections(IndexSet(integer: index), with: .automatic)
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
})
|
119
|
+
|
120
|
+
alert.addAction(cancelAction)
|
121
|
+
alert.addAction(defaultAction)
|
122
|
+
alert.addTextField(configurationHandler: { text -> Void in })
|
123
|
+
present(alert, animated: true, completion: nil)
|
124
|
+
|
125
|
+
|
126
|
+
}
|
127
|
+
|
128
|
+
// Rowを追加する(アニメーションはご自由に)
|
129
|
+
@IBAction func addRow(_ sender: UIButton) {
|
130
|
+
//func addRow() {
|
131
|
+
|
132
|
+
if dataArrayGroup.count == 0 {
|
133
|
+
return
|
134
|
+
}
|
135
|
+
|
136
|
+
let alert = UIAlertController(title:"Row追加",
|
137
|
+
message: "個数と金額を入力してください",
|
138
|
+
preferredStyle: .alert)
|
139
|
+
|
140
|
+
let cancelAction = UIAlertAction(title: "Cancel",
|
141
|
+
style: .cancel,
|
142
|
+
handler:
|
143
|
+
{ action -> Void in
|
144
|
+
print("Cancel")
|
145
|
+
})
|
146
|
+
|
147
|
+
let count = dataArrayGroup[0].count
|
148
|
+
let defaultAction = UIAlertAction(title: "OK",
|
149
|
+
style: .default,
|
150
|
+
handler:
|
151
|
+
{ action -> Void in
|
152
|
+
|
153
|
+
// TextFieldから値を取得
|
154
|
+
if let textFields = alert.textFields {
|
155
|
+
|
156
|
+
let data = DataModel()
|
157
|
+
|
158
|
+
for textField in textFields {
|
159
|
+
|
160
|
+
if let text = textField.text, !text.isEmpty, let _ = Int(text) {
|
161
|
+
if textField.tag == 1 {
|
162
|
+
data.count = text
|
163
|
+
} else {
|
164
|
+
data.price = text
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
self.dataArrayGroup[0].insert(data, at: count)
|
170
|
+
self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic)
|
171
|
+
}
|
172
|
+
})
|
173
|
+
|
174
|
+
|
175
|
+
alert.addTextField {
|
176
|
+
$0.placeholder = "個数"
|
177
|
+
$0.keyboardType = .numberPad
|
178
|
+
$0.tag = 1
|
179
|
+
}
|
180
|
+
alert.addTextField {
|
181
|
+
$0.placeholder = "金額"
|
182
|
+
$0.keyboardType = .numberPad
|
183
|
+
$0.tag = 2
|
184
|
+
}
|
185
|
+
|
186
|
+
alert.addAction(cancelAction)
|
187
|
+
alert.addAction(defaultAction)
|
188
|
+
present(alert, animated: true, completion: nil)
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// MARK: - TableView Delegate & DataSource
|
194
|
+
//この部分です。
|
195
|
+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
196
|
+
if sectionTitleArray.count == 0 {
|
197
|
+
return nil
|
198
|
+
} else {
|
199
|
+
return sectionTitleArray[section]
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
// Section Count
|
204
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
205
|
+
return dataArrayGroup.count
|
206
|
+
}
|
207
|
+
|
208
|
+
// Row Count
|
209
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
210
|
+
return dataArrayGroup[section].count
|
211
|
+
}
|
212
|
+
|
213
|
+
// Generate Cell
|
214
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
215
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
|
216
|
+
cell.data = dataArrayGroup[indexPath.section][indexPath.row]
|
217
|
+
cell.indexLabel.text = String(indexPath.row + 1)
|
218
|
+
return cell
|
219
|
+
}
|
220
|
+
|
221
|
+
// Select Cell
|
222
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
223
|
+
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
|
224
|
+
}
|
225
|
+
|
226
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
227
|
+
if editingStyle == UITableViewCellEditingStyle.delete {
|
228
|
+
dataArrayGroup[indexPath.section].remove(at: indexPath.row)
|
229
|
+
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
//cellが削除が可能なことを伝える
|
234
|
+
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
|
235
|
+
return UITableViewCellEditingStyle.delete;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
```
|
239
|
+
|
11
240
|
参考文献
|
12
241
|
KentarouさんのGitHub
|