質問編集履歴
7
細かい修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
TableViewのアコーディオンHeaderのアニメーションの挙動がおかしい
|
1
|
+
TableViewのアコーディオンCustomHeaderのアニメーションの挙動がおかしい
|
test
CHANGED
@@ -16,12 +16,18 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
ちなみにHeaderは複数のラベルなどを設置しているカスタムHeaderです。
|
20
|
+
|
21
|
+
|
22
|
+
|
19
23
|
必要と思われる部分のコードを抜き出しました。
|
20
24
|
|
21
25
|
|
22
26
|
|
23
27
|
|
24
28
|
|
29
|
+
|
30
|
+
|
25
31
|
```Swift
|
26
32
|
|
27
33
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
6
細かい修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -484,6 +484,8 @@
|
|
484
484
|
|
485
485
|
|
486
486
|
|
487
|
+
~~~省略~~~
|
488
|
+
|
487
489
|
|
488
490
|
|
489
491
|
```
|
5
Sectionをタップした際にコンソールに表示される制約に関するメッセージを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,14 +6,6 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
下に自作アプリのgifを貼ります。
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
上の方にあるSectionはキレイに開閉してくれるのですが、下の方にあるものはおかしな挙動になります。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
9
|
無知なりに数日間ずっと解決法を調べて試してみましたが解決に至りませんでした。
|
18
10
|
|
19
11
|
|
@@ -198,8 +190,6 @@
|
|
198
190
|
|
199
191
|
repeatIconImageView.image = UIImage(named: "repeat")
|
200
192
|
|
201
|
-
// repeatIconImageView.sizeToFit()
|
202
|
-
|
203
193
|
}else {
|
204
194
|
|
205
195
|
repeatIconImageView = UIImageView(frame: CGRect(x: 50,y: 10,width: 0 ,height: 0))
|
@@ -270,14 +260,8 @@
|
|
270
260
|
|
271
261
|
if date! < Date() {
|
272
262
|
|
273
|
-
// dateLabel.textColor = UIColor(hex: "CC1414")
|
274
|
-
|
275
263
|
dateLabel.textColor = .red
|
276
264
|
|
277
|
-
// dateLabel.textColor = .lightGray
|
278
|
-
|
279
|
-
// dateLabel.text! += " 期限超過"
|
280
|
-
|
281
265
|
sectionView.backgroundColor = .quaternaryLabel
|
282
266
|
|
283
267
|
|
@@ -404,8 +388,6 @@
|
|
404
388
|
|
405
389
|
memoLabel.frame = CGRect(x: 50,y: sectionTitle.frame.maxY,width: 0 ,height: 0)
|
406
390
|
|
407
|
-
// memoLabel.sizeToFit()
|
408
|
-
|
409
391
|
}
|
410
392
|
|
411
393
|
|
@@ -486,6 +468,32 @@
|
|
486
468
|
|
487
469
|
|
488
470
|
|
471
|
+
Sectionをタップしたときにコンソールに表示されるメッセージ
|
472
|
+
|
473
|
+
不正な制約という内容のメッセージですが、もしかするとこれが関係ありそうな気がしています。
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
```
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
[LayoutConstraints] Unable to simultaneously satisfy constraints.
|
482
|
+
|
483
|
+
Probably at least one of the constraints in the following list is one you don't want.
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
```
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
|
496
|
+
|
489
497
|
reloadSectionsの挙動
|
490
498
|
|
491
499
|
|
4
viewForHeaderInSectionメソッドの中身の追記、gifの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -142,6 +142,350 @@
|
|
142
142
|
|
143
143
|
```
|
144
144
|
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
追記
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
SectionのViewを返すviewForHeaderInSectionメソッドの中身を追加させていただきます。
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
```Swift
|
158
|
+
|
159
|
+
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
160
|
+
|
161
|
+
let sectionView = UIView(frame:CGRect(x: 0,y: 0,width: 0,height: 0))
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
let vividColors = ["FF7F3B","FF96D7","FF279F","BE00E8","5A2199","2D23D2","FF433B","3DB2FA"]
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
sectionView.backgroundColor = UIColor(hex: vividColors[Int.random(in: 0..<vividColors.count)])
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
let dateLabel = UILabel(frame:CGRect(x: 50,y: 10,width: self.view.frame.width - 100 ,height: 20))
|
182
|
+
|
183
|
+
dateLabel.font = UIFont.boldSystemFont(ofSize: 20)
|
184
|
+
|
185
|
+
dateLabel.textColor = .white
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
var repeatIconImageView: UIImageView = UIImageView()
|
192
|
+
|
193
|
+
if taskItems[section].details[2] != "なし" {
|
194
|
+
|
195
|
+
dateLabel.frame = CGRect(x: 70,y: 10,width: self.view.frame.width - 100 ,height: 20)
|
196
|
+
|
197
|
+
repeatIconImageView = UIImageView(frame: CGRect(x: 50,y: 13,width: 15 ,height: 15))
|
198
|
+
|
199
|
+
repeatIconImageView.image = UIImage(named: "repeat")
|
200
|
+
|
201
|
+
// repeatIconImageView.sizeToFit()
|
202
|
+
|
203
|
+
}else {
|
204
|
+
|
205
|
+
repeatIconImageView = UIImageView(frame: CGRect(x: 50,y: 10,width: 0 ,height: 0))
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
if taskItems[section].details[1] != "なし" {
|
214
|
+
|
215
|
+
let a = taskItems[section].details[1].replace("年", "/")
|
216
|
+
|
217
|
+
let b = a.replace("月", "/")
|
218
|
+
|
219
|
+
let c = b.replace("日", "")
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
let formatter = DateFormatter()
|
224
|
+
|
225
|
+
formatter.locale = Locale(identifier: "ja_JP")
|
226
|
+
|
227
|
+
formatter.dateFormat = "yyyy/MM/dd HH:mm"
|
228
|
+
|
229
|
+
let date = formatter.date(from: c)
|
230
|
+
|
231
|
+
let calendar = Calendar.current
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
let isSameDate = calendar.isDate(date!, inSameDayAs: Date())
|
238
|
+
|
239
|
+
let isTomorrow = calendar.isDate(date!, inSameDayAs: calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: Date()))!)
|
240
|
+
|
241
|
+
let isDayAfterTomorrow = calendar.isDate(date!, inSameDayAs: calendar.date(byAdding: .day, value: 2, to: calendar.startOfDay(for: Date()))!)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if isSameDate {
|
246
|
+
|
247
|
+
formatter.dateFormat = "今日 HH:mm"
|
248
|
+
|
249
|
+
dateLabel.text = formatter.string(from: date!)
|
250
|
+
|
251
|
+
}else if isTomorrow {
|
252
|
+
|
253
|
+
formatter.dateFormat = "明日 HH:mm"
|
254
|
+
|
255
|
+
dateLabel.text = formatter.string(from: date!)
|
256
|
+
|
257
|
+
}else if isDayAfterTomorrow {
|
258
|
+
|
259
|
+
formatter.dateFormat = "明後日 HH:mm"
|
260
|
+
|
261
|
+
dateLabel.text = formatter.string(from: date!)
|
262
|
+
|
263
|
+
}else {
|
264
|
+
|
265
|
+
dateLabel.text = taskItems[section].details[1]
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
if date! < Date() {
|
272
|
+
|
273
|
+
// dateLabel.textColor = UIColor(hex: "CC1414")
|
274
|
+
|
275
|
+
dateLabel.textColor = .red
|
276
|
+
|
277
|
+
// dateLabel.textColor = .lightGray
|
278
|
+
|
279
|
+
// dateLabel.text! += " 期限超過"
|
280
|
+
|
281
|
+
sectionView.backgroundColor = .quaternaryLabel
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
dateLabel.text! += " 【超過!!】"
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
}else {
|
294
|
+
|
295
|
+
dateLabel.text = "日時設定なし"
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
sectionView.tag = section
|
304
|
+
|
305
|
+
var sectionTitle: UILabel!
|
306
|
+
|
307
|
+
sectionTitle = UILabel(frame:CGRect(x: 50,y: 40,width: self.view.frame.width - 100 ,height: 20))
|
308
|
+
|
309
|
+
sectionTitle.font = UIFont.boldSystemFont(ofSize: 18)
|
310
|
+
|
311
|
+
sectionTitle.text = taskItems[section].taskName
|
312
|
+
|
313
|
+
sectionTitle.textColor = .white
|
314
|
+
|
315
|
+
sectionTitle.numberOfLines = 0
|
316
|
+
|
317
|
+
sectionTitle.sizeToFit()
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
// let tickBtn = UIButton(frame: CGRect(x: 10,y: sectionTitle.frame.maxY - 15,width: 30 ,height: 30))
|
322
|
+
|
323
|
+
let tickBtn = UIButton(frame: CGRect(x: 10,y: 10,width: 30 ,height: 30))
|
324
|
+
|
325
|
+
tickBtn.setImage(UIImage(named: taskItems[section].isComplete ? "interfaces" : "done"), for: .normal)
|
326
|
+
|
327
|
+
tickBtn.tag = section
|
328
|
+
|
329
|
+
tickBtn.addTarget(self, action: #selector(self.tickBtnAction(_:)), for: .touchDown)
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
let priorityLabel: UILabel = UILabel()
|
334
|
+
|
335
|
+
if taskItems[section].details[5] != "なし" {
|
336
|
+
|
337
|
+
priorityLabel.textColor = .systemBlue
|
338
|
+
|
339
|
+
priorityLabel.textAlignment = .center
|
340
|
+
|
341
|
+
priorityLabel.frame = CGRect(x: 50, y: 40, width: 15, height: 15)
|
342
|
+
|
343
|
+
sectionTitle.frame = CGRect(x: 70,y: 40,width: self.view.frame.width - 100 ,height: 20)
|
344
|
+
|
345
|
+
sectionTitle.sizeToFit()
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
switch taskItems[section].details[5] {
|
352
|
+
|
353
|
+
case "低":
|
354
|
+
|
355
|
+
priorityLabel.text = "!"
|
356
|
+
|
357
|
+
case "中":
|
358
|
+
|
359
|
+
priorityLabel.text = "!!"
|
360
|
+
|
361
|
+
case "高":
|
362
|
+
|
363
|
+
priorityLabel.text = "!!!"
|
364
|
+
|
365
|
+
default: break
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
priorityLabel.sizeToFit()
|
370
|
+
|
371
|
+
}else {
|
372
|
+
|
373
|
+
priorityLabel.frame = CGRect(x: 50, y: 50, width: 0, height: 0)
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
sectionView.layer.cornerRadius = 15
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
let memoLabel = UILabel()
|
386
|
+
|
387
|
+
if taskItems[section].details[3] != "" {
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
memoLabel.frame = CGRect(x: 50,y: (sectionTitle.frame.maxY + 10),width: self.view.frame.width - 100 ,height: 50)
|
392
|
+
|
393
|
+
memoLabel.numberOfLines = 0
|
394
|
+
|
395
|
+
memoLabel.font = UIFont.boldSystemFont(ofSize: 15)
|
396
|
+
|
397
|
+
memoLabel.textColor = .white
|
398
|
+
|
399
|
+
memoLabel.text = taskItems[section].details[3]
|
400
|
+
|
401
|
+
memoLabel.sizeToFit()
|
402
|
+
|
403
|
+
}else {
|
404
|
+
|
405
|
+
memoLabel.frame = CGRect(x: 50,y: sectionTitle.frame.maxY,width: 0 ,height: 0)
|
406
|
+
|
407
|
+
// memoLabel.sizeToFit()
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
let urlBtn = UIButton(type: .system)
|
414
|
+
|
415
|
+
if taskItems[section].details[4] != "" {
|
416
|
+
|
417
|
+
urlBtn.frame = CGRect(x: 50,y: memoLabel.frame.maxY,width: self.view.frame.width - 100 ,height: 20)
|
418
|
+
|
419
|
+
urlBtn.tag = section
|
420
|
+
|
421
|
+
urlBtn.titleLabel!.font = UIFont.boldSystemFont(ofSize: 15)
|
422
|
+
|
423
|
+
urlBtn.contentHorizontalAlignment = .left
|
424
|
+
|
425
|
+
urlBtn.addTarget(self, action: #selector(self.urlBtnAction(_:)), for: .touchDown)
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
if let component: NSURLComponents = NSURLComponents(string: taskItems[section].details[4]) {
|
430
|
+
|
431
|
+
urlBtn.setTitle(component.host ?? taskItems[section].details[4], for: .normal)
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
urlBtn.sizeToFit()
|
436
|
+
|
437
|
+
}else {
|
438
|
+
|
439
|
+
urlBtn.frame = CGRect(x: 50,y: memoLabel.frame.maxY,width: 0 ,height: 10)
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
//セクションにタップジェスチャーを追加
|
448
|
+
|
449
|
+
sectionView.addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(sectionTapAction(_:))))
|
450
|
+
|
451
|
+
sectionView.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(sectionLongTapAction(_:))))
|
452
|
+
|
453
|
+
sectionView.addSubview(tickBtn)
|
454
|
+
|
455
|
+
sectionView.addSubview(dateLabel)
|
456
|
+
|
457
|
+
sectionView.addSubview(sectionTitle)
|
458
|
+
|
459
|
+
sectionView.addSubview(memoLabel)
|
460
|
+
|
461
|
+
sectionView.addSubview(repeatIconImageView)
|
462
|
+
|
463
|
+
sectionView.addSubview(priorityLabel)
|
464
|
+
|
465
|
+
sectionView.addSubview(urlBtn)
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
urlBtn.bottomAnchor.constraint(equalTo: sectionView.bottomAnchor, constant: 0).isActive = true
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
return sectionView
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
```
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
145
489
|
reloadSectionsの挙動
|
146
490
|
|
147
491
|
|
@@ -155,3 +499,11 @@
|
|
155
499
|
|
156
500
|
|
157
501
|
![イメージ説明](4973730b32848da338d29de694442017.gif)
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
スローモーションで動きを捉えてみました。
|
508
|
+
|
509
|
+
![イメージ説明](f23a64face9e2f0d145fffc5bc1d2057.gif)
|
3
gifの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -142,6 +142,16 @@
|
|
142
142
|
|
143
143
|
```
|
144
144
|
|
145
|
+
reloadSectionsの挙動
|
146
|
+
|
145
147
|
|
146
148
|
|
147
149
|
![![](9e8e3a1fadb745b19e520988eae37f54.gif)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
insertRowsおよびdeleteRowsの挙動
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
![イメージ説明](4973730b32848da338d29de694442017.gif)
|
2
回答を参考にコードの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,6 +42,8 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
+
// Sectionがタップされたときの処理
|
46
|
+
|
45
47
|
@objc func sectionTapAction(_ sender: UIGestureRecognizer) {
|
46
48
|
|
47
49
|
if let section = sender.view?.tag {
|
@@ -68,4 +70,78 @@
|
|
68
70
|
|
69
71
|
|
70
72
|
|
73
|
+
追記
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
頂いた回答を踏まえて、TableView.reloadSectionsを
|
78
|
+
|
79
|
+
TableView.insertRowsおよびTableView.deleteRowsに変更してみました。
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
しかしまだおかしな挙動は改善されませんでした。
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
```Swift
|
88
|
+
|
89
|
+
// Sectionがタップされたときの処理
|
90
|
+
|
91
|
+
@objc func sectionTapAction(_ sender: UIGestureRecognizer) {
|
92
|
+
|
93
|
+
if let section = sender.view?.tag {
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
taskItems[section].taskName = taskItems[section].details[0]
|
98
|
+
|
99
|
+
selectedSectionIndex = section
|
100
|
+
|
101
|
+
self.taskItems[self.selectedSectionIndex].isOpen = !self.taskItems[self.selectedSectionIndex].isOpen
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
var indexPaths = [IndexPath]()
|
106
|
+
|
107
|
+
for row in taskItems[section].details.indices {
|
108
|
+
|
109
|
+
let indexPath = IndexPath(row: row, section: section)
|
110
|
+
|
111
|
+
indexPaths.append(indexPath)
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
if taskItems[selectedSectionIndex].isOpen {
|
124
|
+
|
125
|
+
tableView.insertRows(at: indexPaths, with: .none)
|
126
|
+
|
127
|
+
tableView.endUpdates()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
}else {
|
132
|
+
|
133
|
+
tableView.deleteRows(at: indexPaths, with: .none)
|
134
|
+
|
135
|
+
tableView.endUpdates()
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
|
71
147
|
![![](9e8e3a1fadb745b19e520988eae37f54.gif)
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,9 +24,45 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
必要と思われる部分のコードを抜き出しました。
|
27
28
|
|
28
29
|
|
29
30
|
|
31
|
+
|
32
|
+
|
33
|
+
```Swift
|
34
|
+
|
35
|
+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
return taskItems[section].isOpen ? taskItems[section].details.count : 0
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
@objc func sectionTapAction(_ sender: UIGestureRecognizer) {
|
46
|
+
|
47
|
+
if let section = sender.view?.tag {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
taskItems[section].taskName = taskItems[section].details[0]
|
52
|
+
|
53
|
+
selectedSectionIndex = section
|
54
|
+
|
55
|
+
self.taskItems[self.selectedSectionIndex].isOpen = !self.taskItems[self.selectedSectionIndex].isOpen
|
56
|
+
|
57
|
+
self.tableView.reloadSections(IndexSet(integer: self.selectedSectionIndex) , with: .automatic)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
```
|
30
66
|
|
31
67
|
|
32
68
|
|