Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.