带有备用背景颜色的 SwiftUI 列表

Did*_*ids 6 list background-color ios swiftui

在 SwiftUI 中,TableView 被替换为 List。

有没有办法改变列表单元格/行的背景颜色?

我想实现这样的东西,

if (indexPath.row % 2 == 0) { 
    aCell.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1.0) 
}
Run Code Online (Sandbox Code Playgroud)

有关一个很好的例子,请参阅文章https://medium.com/@ronm333/improving-the-appearance-of-ios-tableviews-9effb7184efb

Aus*_*lon 21

SwiftUI 现在通过inset(alternatesRowBackgrounds:).

.listStyle(.inset(alternatesRowBackgrounds: true))


小智 7

我使用这些方法在我的 SwiftUI 列表中交替列表背景颜色

List {
    ForEach(items.indices) { index in
        Text(items[index])
            .listRowBackground((index  % 2 == 0) ? Color(.systemBlue) : Color(.white))
    }
}
Run Code Online (Sandbox Code Playgroud)