ios - 在 SwiftUI 中更改 UITableViewCell 背景颜色
问题描述
我使用列表来显示动态数量的自定义视图。见代码。
@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)
}
在那之后,我有了这个清单。
之后,我像这样修改了列表。
UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().backgroundColor = UIColor.clear
列表底部现在已更改为清除,但单元格仍然是白色的。
如果我这样编辑它:
CoinEntry(coin: coins[i]).background(MAIN_DARK_GRAY)
该列表如下所示:
希望有人能帮忙!
解决方案
只需添加 .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)
}
推荐阅读
- javascript - 具有固定长度的 Svelte 字段
- python - 如何在python中显示/打印类方法
- c# - 我正在尝试跳过 gplex(flex/lex 端口)中的令牌,并且使用 yylex() 会导致堆栈溢出。有没有更好的跳过方法?
- matlab - 如何将 xticks 和 yticks 与常数相乘并将其显示在 Matlab 2019 的图中?
- javascript - 从外部源加载三个Js?
- yaml - 如何使用其他变量在 Github Actions 中创建环境变量
- java - 如何在java(wsl)中设置路径?
- android - 我可以在 Room 中添加检查约束吗?
- amazon-web-services - AWS Loadbalancer 在发送完整的 http 响应之前终止 http 调用
- google-cloud-platform - 为什么 GCE 负载均衡器通过域名和 IP 地址表现不同?