回答編集履歴
1
再描画に関するコードを追記しました。
test
CHANGED
@@ -30,3 +30,93 @@
|
|
30
30
|
|
31
31
|
https://developer.apple.com/documentation/swiftui/foreach/init(_:content:)-6oy5i
|
32
32
|
|
33
|
+
---
|
34
|
+
|
35
|
+
### ********** 追記です。 **********
|
36
|
+
|
37
|
+
コメントありがとうございます。
|
38
|
+
連日遅くまで頑張りましたね。
|
39
|
+
結構根本的なところからの変更になりそうでしたが、1日でできてしまったのですね、すごいです。
|
40
|
+
|
41
|
+
> 再描画することは難しいのでしょうか。
|
42
|
+
|
43
|
+
最小限のコードで再現確認しました。
|
44
|
+
|
45
|
+
`FSCalendar.reloadData()`で再読み込みしてくれるみたいですので、
|
46
|
+
`updateUIView(_:context:)`の中でarrayを最新にしてあげてから呼び出してあげると良いと思います。
|
47
|
+
|
48
|
+
https://github.com/WenchaoD/FSCalendar/blob/c4a443ac79bd62e483093aa3916aaa7347f1645e/MOREUSAGE.md#qcan-we-refresh-the-calendar-after-a-network-request
|
49
|
+
|
50
|
+
@BindginはViewプロトコルに準拠した構造体の中に記述するのが一般的と思いました。
|
51
|
+
SwiftUIのチュートリアルも見てみると良いかもしれません。
|
52
|
+
|
53
|
+
Section 6 Create a Favorite Button for Each Landmark > Step 2
|
54
|
+
https://developer.apple.com/tutorials/swiftui/handling-user-input
|
55
|
+
|
56
|
+
```swift
|
57
|
+
import SwiftUI
|
58
|
+
import FSCalendar
|
59
|
+
|
60
|
+
struct CalendarPage:View{
|
61
|
+
@State var selectedDate=Date()
|
62
|
+
@State var array=[Date]()
|
63
|
+
var body: some View{
|
64
|
+
Calendarpage(selectedDate:$selectedDate,array:$array)
|
65
|
+
Button("+", action: action)
|
66
|
+
}
|
67
|
+
func action() {
|
68
|
+
let calendar = Calendar.current
|
69
|
+
let date1 = Date()
|
70
|
+
guard let date2 = calendar.date(byAdding: .day, value: array.count + 1, to: date1) else {
|
71
|
+
return
|
72
|
+
}
|
73
|
+
let components1 = calendar.dateComponents([.year, .month, .day], from: date2)
|
74
|
+
guard let date3 = calendar.date(from: components1) else {
|
75
|
+
return
|
76
|
+
}
|
77
|
+
array.append(date3)
|
78
|
+
}
|
79
|
+
}
|
80
|
+
struct Calendarpage: UIViewRepresentable{
|
81
|
+
@Binding var selectedDate:Date
|
82
|
+
@Binding var array:[Date]
|
83
|
+
func makeUIView(context:Context)->UIView{
|
84
|
+
typealias UIViewType=FSCalendar
|
85
|
+
let fsCalendar=FSCalendar()
|
86
|
+
fsCalendar.delegate=context.coordinator
|
87
|
+
fsCalendar.dataSource=context.coordinator
|
88
|
+
return fsCalendar
|
89
|
+
}
|
90
|
+
func updateUIView(_ uiView:UIView,context:Context){
|
91
|
+
// ***** FSCalendar.reloadDataで再読み込みしてくれるみたいです
|
92
|
+
guard let v = uiView as? FSCalendar else {
|
93
|
+
return
|
94
|
+
}
|
95
|
+
context.coordinator.array = array
|
96
|
+
v.reloadData()
|
97
|
+
}
|
98
|
+
func makeCoordinator()->Coordinator{
|
99
|
+
return Coordinator(self,array:array)
|
100
|
+
}
|
101
|
+
class Coordinator: NSObject,FSCalendarDelegateAppearance,FSCalendarDataSource{
|
102
|
+
// ***** arrayは @Binding から通常のプロパティにしました
|
103
|
+
var array:[Date]
|
104
|
+
var parent:Calendarpage
|
105
|
+
init(_ parent:Calendarpage,array:[Date]){
|
106
|
+
self.parent=parent
|
107
|
+
self.array=array
|
108
|
+
}
|
109
|
+
func calendar(_ calendar:FSCalendar,didSelect date:Date,at monthPosition:FSCalendarMonthPosition){
|
110
|
+
parent.selectedDate=date
|
111
|
+
}
|
112
|
+
func calendar(_ calendar:FSCalendar,numberOfEventsFor date:Date)->Int{
|
113
|
+
if array.contains(date){
|
114
|
+
return 1
|
115
|
+
} else {
|
116
|
+
return 0
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
```
|
122
|
+
|