Make a list row selectable in SwiftUI

Jak*_*aps 4 ios swift swiftui

I'm creating a list with custom rows in SwiftUI. When I tap one of them, I want the row to turn light gray until another tap is detected.

This doesn't happen with simple non-custom lists either.

Here's my list:

List{
        ForEach(blocks){ block in
            BlockRow(block: block)
        }
        .onDelete(perform: delete)
        .onMove(perform: day.move)
}
Run Code Online (Sandbox Code Playgroud)

When I tap on one of the items, nothing happens. If I create a simple list with storyBoards, I get the behavior I want:

期望的行为

nix*_*des 7

嘿,所以你 3 个月前问过这个问题,所以我希望你能在某个地方得到答案或从那时起就想通了,但是为了让按钮可点击,我能够使用它让它工作,

List {
    Button (action: {
        //Whatever action you want to perform
    }) {
        //Code to present as the cell
    }
}
Run Code Online (Sandbox Code Playgroud)

我可能会根据您的代码尝试以下操作,

List (blocks) { block in
  Button (action: {
    //perform button action
  }) {
    //How the cell should look
    BlockRow(block: block)
  }
}
.onAppear()
.onDelete()
Run Code Online (Sandbox Code Playgroud)