在 SwiftUI 中更改 UITableViewCell 背景颜色

时间:2021-06-08 12:53:10

标签: ios swift swiftui

我使用一个列表来显示动态数量的自定义视图。查看代码。

 @State var coins : [Coin]
var coinList : some View {
        List {
            ForEach(0..<coins.count, id: \.self) { i in
                    CoinEntry(coin: coins[i])
                }.onDelete(perform: { indexSet in
                    self.coins.remove(atOffsets: indexSet)
                })
        }.frame(width: UIScreen.main.bounds.width)
    }

在那之后,我有了这份清单。

enter image description here

之后我像这样修改了列表。

UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().backgroundColor = UIColor.clear

列表底部现在已更改为清除,但单元格仍为白色。

enter image description here

如果我像这样编辑它:

CoinEntry(coin: coins[i]).background(MAIN_DARK_GRAY)

列表如下所示:

enter image description here

希望有人能帮忙!

1 个答案:

答案 0 :(得分:2)

只需添加 .background 属性即可更改视图的背景颜色

var coinList : some View {
    List {
        ForEach(0..<coins.count, id: \.self) { i in
                CoinEntry(coin: coins[i]).background(MAIN_DARK_GRAY)
            }.onDelete(perform: { indexSet in
                self.coins.remove(atOffsets: indexSet)
            })
    }.frame(width: UIScreen.main.bounds.width).background(MAIN_DARK_GRAY)
}

或者你可以使用 listRowBackground 属性

var coinList : some View {
    List {
        ForEach(0..<coins.count, id: \.self) { i in
                CoinEntry(coin: coins[i])
            }.onDelete(perform: { indexSet in
                self.coins.remove(atOffsets: indexSet)
            })
    }.frame(width: UIScreen.main.bounds.width).listRowBackground(MAIN_DARK_GRAY)
}