SwiftUI: EditButton does not trigger onDelete when embedded within an HStack Section header

て烟熏妆下的殇ゞ 提交于 2021-01-28 13:33:29

问题


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

enter image description here

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!