问题
I have a Form Section which I want to show some text and the EditButton() within the same header line as shown below:
The issue occurs when I tap the button whenever its embedded within an HStack. The button text toggles between "Edit" and "Done", yet it doesn't call the onDelete() action for the rows. However, it does work if it's solely assigned as the header, footer, or embedded in a Group arrangement for the Section.
Section(header: HStack { Text("Recent"); Spacer(); EditButton() }) {
ForEach(locationsList, id:\.self) { location in
Text("\(location.name)")
}.onDelete(perform: deleteLocation)
}
Does anyone have any reasoning why my ForEach loop wouldn't be responding to the button when it's embedded in a view arrangement such as an HStack, VStack, or even a ZStack? Is there an alternative to achieve the same layout for the header without using an HStack?
回答1:
Looks like it's because EditButton is inside list. It works if to move EditButton out of List, like below

VStack {
HStack { Text("Recent"); Spacer(); EditButton() }
.padding(.horizontal)
.background(Color(UIColor.systemGray3))
List{
ForEach(locationsList, id:\.self) { location in
...
来源:https://stackoverflow.com/questions/59851275/swiftui-editbutton-does-not-trigger-ondelete-when-embedded-within-an-hstack-sec