0

I have a list of expandable items, which I made following along this post: https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-expanding-lists

Here is the code:

struct Examples: View {
    
    private struct Example: Identifiable {
        let id = UUID()
        let string: String
        var items: [Example]?
    }
    
    private let exampleItems: [Example] = [
        Example(string: "One",
                items: [Example(string: "1")]),
        
        Example(string: "Two",
                items: [Example(string: "2")]),
        
        Example(string: "Three",
                items: [Example(string: "3")]),
    ]
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            List(exampleItems, children: \.items) { row in
                Text(row.string)
            }
        }
    }
}

And I'd like to add a bit of haptic feedback each time a row is expanded or minimised. In UIKit you'd just pop this in tableView(_:didSelectRowAt:), and that would be that.

However I can't figure out the SwiftUI equivalent, and all my attempts to make things tappable or add buttons etc. break the main action of expanding the list row.

How does one perform an action upon a row being expanded/minimised in SwiftUI?

Jon Cox
  • 10,622
  • 22
  • 78
  • 123
  • `.onTapGesture` works very similar to that method. – lorem ipsum Jun 05 '22 at 14:13
  • It is better to do that not with default (opaque) hierarchy but having explicit access to DisclosureGroups, like in https://stackoverflow.com/a/63228810/12299030. – Asperi Jun 05 '22 at 14:18

0 Answers0