Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a list row selectable in SwiftUI

Tags:

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)
}

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

desired behaviour

like image 905
Jakkaps Avatar asked Mar 04 '23 17:03

Jakkaps


1 Answers

Hey so you asked this 3 months ago so I hope you got an answer somewhere or figured it out since then, but to get to the good stuff to make a button tappable I was able to get it working using this,

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

I would maybe try the following based on your code,

List (blocks) { block in
  Button (action: {
    //perform button action
  }) {
    //How the cell should look
    BlockRow(block: block)
  }
}
.onAppear()
.onDelete()
like image 116
nix.codes Avatar answered Mar 20 '23 01:03

nix.codes