[ad_1]
I’ve a essential view in my app, that will get a array of things from CloudKit by question. I then use a foreach to indicate a listing of things. Now, if I create the “row” contained in the listing and I replace knowledge on one other gadget, the listing will get up to date with new knowledge.
But when I create a subview of the “row”, it doesn’t. I’ve tried looking out throughout Stack and Google, however in all places the unique array of things is a @State variable after which the contents are already @Binding. But when I am going through @Question, I can not determine, easy methods to go on the objects to the subview as @Binding.
This manner works completely:
struct ItemsView: View {
@Surroundings(.modelContext) personal var modelContext
@Question(kind: [SortDescriptor(Item.name)) private var items: [Item]
var physique: some View {
NavigationStack {
Listing {
ForEach(objects) { merchandise in
ZStack(alignment: .main) {
NavigationLink {
EditView(merchandise: merchandise)
} label: {
EmptyView()
}
HStack(spacing: 8) {
Picture(systemName: "photograph.circle.fill")
.resizable()
.scaledToFit()
.body(width: 48, top: 48)
.opacity(0.08)
VStack(alignment: .main) {
Textual content(merchandise.title).font(.physique)
if let desc = merchandise.description,
!desc.isEmpty {
Textual content(desc).font(.footnote)
}
}
}
}
}
}
}
}
}
But when I cut up it right into a separate subview, the view doesn’t get up to date if the information modifications.
struct ItemsView: View {
@Surroundings(.modelContext) personal var modelContext
@Question(kind: [SortDescriptor(Item.name)) private var items: [Item]
var physique: some View {
NavigationStack {
Listing {
ForEach(objects) { merchandise in
ZStack(alignment: .main) {
NavigationLink {
EditView(merchandise: merchandise)
} label: {
EmptyView()
}
ItemRow(merchandise: merchandise)
}
}
}
}
}
}
struct ItemRow: View {
var merchandise: Merchandise
var physique: some View {
HStack(spacing: 8) {
Picture(systemName: "photograph.circle.fill")
.resizable()
.scaledToFit()
.body(width: 48, top: 48)
.opacity(0.08)
VStack(alignment: .main) {
Textual content(merchandise.title).font(.physique)
if let desc = merchandise.description,
!desc.isEmpty {
Textual content(desc).font(.footnote)
}
}
}
}
}
I’ve tried utilizing @Binding in subview, however then I get an error, that merchandise
will not be Binding. I additionally tried casting merchandise to @Bindable as in a single instance contained in the foreach (@Bindable var merchandise = merchandise
), however this doesn’t wor both and doesn’t compile.
I’m on XCode 15 and iOS 17.
[ad_2]