Ask Your Question
1

In SwiftUI, what is the method for modifying the color of a selected row in a table?

asked 2021-04-07 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-05-25 05:00:00 +0000

qstack gravatar image

The color of a selected row in a table in SwiftUI can be modified by setting the background color of the row using the listRowBackground modifier. For example:

List {
    ForEach(items) { item in
        Text(item)
            .listRowBackground(selectedItem == item ? Color.blue : Color.clear)
    }
}

In this code snippet, the listRowBackground modifier is applied to the Text view within the ForEach loop. The condition selectedItem == item checks if the current item in the loop is the selected item, and if so, sets the background color to blue. Otherwise, the background color is set to clear. selectedItem would be a variable that stores the currently selected item, which can be updated using a @State property.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-07 11:00:00 +0000

Seen: 14 times

Last updated: May 25 '21