Search code examples
swiftuiswiftui-list

How to detect taps on a List cell row in SwiftUI?


Given a basic List with Text, how can i make the whole "cell" from left side of the screen to right, tappable in a List, not just the "Hello world" text?

    List {
         VStack {
             Text("Hello world")
         }    
         .contentShape(Rectangle())
         .onTapGesture {
            print("Tapped cell")  // This only triggers when you directly tap the Text
         }
     }

Solution

  • Add a Button and entire cell is tappable now:

    VStack {
        Button(action: {
            print("Tapped")
        }) {
            Text("Hello world")
        }
    }