SwiftUI - Change List row background colour programatically

Viewed 56

I'm trying to change the background colour of a row in a list based on a value

I have an Int16 value stored in card.owned and I want to change the background if this value is above 0, otherwise use the normal background.

Previously with a table view I could change it with the cellForRowAtIndexPath method, but not sure how to do it with SwiftUI without changing every row

Currently I have

List {
            ForEach(cards) { section in
                let header: String = nameForSectionHeader(sectionID: section.id)
                Section(header: Text(header)) {
                    ForEach(section) { card in
                        NavigationLink(destination: CardView(card: card)) {
                            VStack(alignment: .leading) {
                                if let name = card.name, let id = card.cardID {
                                    Text("\(id) - \(name)")
                                }
                                if let set = card.set, let setName = set.name {
                                    Text("Set: \(setName)")
                                }
                                if card.owned > 0 {
                                    Text("Owned: \(card.owned)")
                                }
                            }
                        }
                    }
                }
            }
            .listRowBackground(lightGreen)
        }
0 Answers
Related