struct AppointmentView: View
{
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Appoint.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Appoint.title , ascending: true)])
var appointments_title: FetchedResults<Appoint>
@State var showAddDate = false
@State var sectionState: [Int: Bool] = [:]
var body: some View
{
NavigationView
{
List
{
ForEach(appointments_title)
{
order in
HStack
{
Text("\(order.title)").font(.headline)
Spacer()
Text("\(order.date, formatter: ContentView.self.taskDateFormat)").font(.headline)
}.contentShape(Rectangle())
.onTapGesture
{
self.sectionState[order] = !self.isExpanded(order)
}
if self.isExpanded(order)
{
Text("\(order.description)”)
}
}
}
.navigationBarTitle("My Appointment")
.navigationBarItems(trailing: Button(action: {self.showAddDate = true}, label: {Image(systemName: "plus.circle").resizable().frame(width: 32, height: 32, alignment: .center)}))
.sheet(isPresented: $showAddDate) {AddDate().environment(\.managedObjectContext, self.managedObjectContext)}
}
}
func isExpanded(_ order:Int) -> Bool
{
sectionState[order] ?? false
}
}
이 코드를 실행하려고하면이 오류 메시지가 표시됩니다.
'FetchedResults <Appoint>'유형의 값을 예상
인수 유형 'Range <Int>' 로 변환 할 수 없습니다.
'FetchedResults <Appoint>'를 'Range <Int>'로 변환하는 방법이 있습니까?
나는 무언가를 알아 냈고 작동합니다. 기본적으로 부울 유형의 다른 속성을 추가하고 .xcdatamodeld 파일에서 'isExpanded'라고 부릅니다. 새 항목을 추가 할 때마다 부울 속성을 false로 설정합니다.
struct AppointmentView: View
{
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Appoint.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Appoint.title , ascending: true)])
var appointments_title: FetchedResults<Appoint>
@State var showAddDate = false
//@State var sectionState: [Int: Bool] = [:]
var body: some View
{
NavigationView
{
List
{
ForEach(appointments_title)
{
order in
HStack
{
Text("\(order.title)").font(.headline)
Spacer()
Text("\(order.date, formatter: ContentView.self.taskDateFormat)").font(.headline)
}.contentShape(Rectangle())
.onTapGesture
{
order.isExpanded.toggle()
}
if order.isExpanded
{
Text("\(order.description)”)
}
else
{
EmptyView()
}
}
}
.navigationBarTitle("My Appointment")
.navigationBarItems(trailing: Button(action: {self.showAddDate = true}, label: {Image(systemName: "plus.circle").resizable().frame(width: 32, height: 32, alignment: .center)}))
.sheet(isPresented: $showAddDate) {AddDate().environment(\.managedObjectContext, self.managedObjectContext)}
}
}
/*func isExpanded(_ order:Int) -> Bool
{
sectionState[order] ?? false
}*/
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다