How to change List's cell background color alternately in SwiftUI?

Hi,


I want to create diffrent cell with seperate color in a section.


Reagards,

Ranjeet





struct AlternateListCellView: View {
    var body: some View {
        List {
            Section(header: Text("Show alternate cell")) {
                ForEach(1...5) { row in
                    Text("Row Index = \(row)")
                }
// "row" variable is out of scope, so its throw compile  time error. Is any other way to create section and cell?
// Kindly correct me if Any mistake.
//                .listRowBackground(row % 2 == 0 ?  Color.orange : Color.yellow)
            }
        }
        .background(Color.purple)
    }
}

Replies

Try moving that modifier to the Text view, and/or using an if statement around the Text view inside the ForEach.