質問編集履歴

2

名前が出ていた

2021/10/03 10:48

投稿

yamato-0123
yamato-0123

スコア2

test CHANGED
File without changes
test CHANGED
@@ -8,18 +8,6 @@
8
8
 
9
9
  ```swift
10
10
 
11
- //
12
-
13
- // ViewController.swift
14
-
15
- // sample6-1
16
-
17
- //
18
-
19
- // Created by 折井隆心 on 2021/10/02.
20
-
21
- //
22
-
23
11
 
24
12
 
25
13
  import UIKit

1

コードを乗せてなかった

2021/10/03 10:48

投稿

yamato-0123
yamato-0123

スコア2

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,385 @@
3
3
  一通りアプリが完成して、シュミレーターを使って修正箇所を探しています。そこで、表の縦列を多くしてみようと思い、多くしたことろ、見出しに使っていた文字が、表に散らばっていくつも表示されてしまいました。そして、アクションをした時に、それは移動して違うところに散らばって表示されしまいます。
4
4
 
5
5
  理由がわかりません。解決方法と、なぜそうなるのか教えていただけないでしょうか。
6
+
7
+
8
+
9
+ ```swift
10
+
11
+ //
12
+
13
+ // ViewController.swift
14
+
15
+ // sample6-1
16
+
17
+ //
18
+
19
+ // Created by 折井隆心 on 2021/10/02.
20
+
21
+ //
22
+
23
+
24
+
25
+ import UIKit
26
+
27
+ import SpreadsheetView
28
+
29
+
30
+
31
+ class ViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
32
+
33
+
34
+
35
+
36
+
37
+ //spreadsheetView
38
+
39
+ @IBOutlet var spreadsheetView: SpreadsheetView!
40
+
41
+ //testScoretextField
42
+
43
+ @IBOutlet var testScoretextField: UITextField!
44
+
45
+
46
+
47
+ var subjectData: [String] = ["数学","国語","英語","社会","理科","物理","科学","化学","古文","現代文","英語2","数学2","国語2","体育","図工","社会2","理科2","物理2","科学2","化学2"]
48
+
49
+
50
+
51
+ var subjecttitle: [String] = ["教科","素点","結果点","平均"]
52
+
53
+
54
+
55
+ var subjectName: [String] = []
56
+
57
+
58
+
59
+ var subjectNameCode: [String] = ["1"]
60
+
61
+
62
+
63
+ var score: [String] = ["0"]
64
+
65
+
66
+
67
+ var count = 0
68
+
69
+
70
+
71
+ var rowCount = 0
72
+
73
+
74
+
75
+ var type: Double = 6
76
+
77
+
78
+
79
+
80
+
81
+ //pickerViewのコード
82
+
83
+
84
+
85
+ //pickeViewの数
86
+
87
+ func numberOfComponents(in pickerView: UIPickerView) -> Int {
88
+
89
+ return 1
90
+
91
+ }
92
+
93
+
94
+
95
+ //pickeViewの中の行の数
96
+
97
+ func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
98
+
99
+ return subjectData.count
100
+
101
+ }
102
+
103
+
104
+
105
+ //pickeViewの中に表示するデータ
106
+
107
+ func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
108
+
109
+ return subjectData[row]
110
+
111
+ }
112
+
113
+
114
+
115
+ //pickeViewで選択されたのを取り出す
116
+
117
+ func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
118
+
119
+
120
+
121
+ rowCount = row
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+ }
130
+
131
+
132
+
133
+ //アプリを起動して画面を開いたら。っていうコード
134
+
135
+ override func viewDidLoad() {
136
+
137
+ // Do any additional setup after loading the view.
138
+
139
+
140
+
141
+ //ここからしたはまるパクリでOK
142
+
143
+ super.viewDidLoad()
144
+
145
+ spreadsheetView.register(MyLabelCell.self, forCellWithReuseIdentifier: MyLabelCell.identifier)
146
+
147
+ spreadsheetView.delegate = self
148
+
149
+ spreadsheetView.dataSource = self
150
+
151
+ view.addSubview(spreadsheetView)
152
+
153
+
154
+
155
+ testScoretextField.placeholder = "点数を入力"
156
+
157
+ }
158
+
159
+
160
+
161
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
162
+
163
+
164
+
165
+ self.spreadsheetView.reloadData()
166
+
167
+ score += [testScoretextField.text!]
168
+
169
+ subjectName = []
170
+
171
+ subjectName = [subjectData[rowCount]]
172
+
173
+ testScoretextField.text = ""
174
+
175
+ if let firstIndex = subjectData.firstIndex(of: subjectName[0]) {
176
+
177
+ print("インデックス番号: (firstIndex)")
178
+
179
+ subjectNameCode += ["(firstIndex + 1)"]
180
+
181
+ count += 1
182
+
183
+ }
184
+
185
+ view.endEditing(true)
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+ func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
200
+
201
+ return 4
202
+
203
+ }
204
+
205
+
206
+
207
+ func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
208
+
209
+ let subjectDatacount = Int(subjectData.count) + 1
210
+
211
+ return subjectDatacount
212
+
213
+ }
214
+
215
+
216
+
217
+ func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
218
+
219
+ return 72
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+ }
228
+
229
+
230
+
231
+ func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
232
+
233
+ return 35
234
+
235
+ }
236
+
237
+
238
+
239
+ func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
240
+
241
+ var i = 0
242
+
243
+ let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+ if indexPath.section == 0 {
252
+
253
+ var i = 0
254
+
255
+ while (i <= subjectData.count){
256
+
257
+ if indexPath.row == i + 1 {
258
+
259
+ cell.setup(with: String(subjectData[i]))
260
+
261
+ }
262
+
263
+ i += 1
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ if indexPath.row == 0 {
272
+
273
+ cell.setup(with: subjecttitle[indexPath.section])
274
+
275
+ }
276
+
277
+
278
+
279
+ if indexPath.section == 1 {
280
+
281
+ while (i <= count){
282
+
283
+ if indexPath.row == Int(subjectNameCode[i]) {
284
+
285
+ cell.setup(with: String(score[i]))
286
+
287
+
288
+
289
+ }
290
+
291
+ i += 1
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+ if indexPath.section == 2 {
300
+
301
+ while (i <= count){
302
+
303
+ if indexPath.row == Int(subjectNameCode[i]) {
304
+
305
+ cell.setup(with: String(atof(score[i]) * type / 10))
306
+
307
+
308
+
309
+ }
310
+
311
+ i += 1
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+ return cell
326
+
327
+ }
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+ class MyLabelCell: Cell {
336
+
337
+ static let identifier = "MyLablelCell"
338
+
339
+
340
+
341
+ private let label = UILabel()
342
+
343
+
344
+
345
+ public func setup(with text: String) {
346
+
347
+ label.text = text
348
+
349
+ label.textAlignment = .center
350
+
351
+ contentView.addSubview(label)
352
+
353
+ }
354
+
355
+
356
+
357
+ override func layoutSubviews() {
358
+
359
+ super.layoutSubviews()
360
+
361
+ label.frame = contentView.bounds
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+ }
378
+
379
+
380
+
381
+
382
+
383
+ ```
384
+
385
+
386
+
387
+ 申し訳ありません。どこを切り取れば良いかわからなかったので、全てコードを書かせていただきました。申し訳ありません。