简体   繁体   English

更改 SwiftUI 中的 UITableViewCell 背景颜色

[英]Change UITableViewCell background color in SwiftUI

I use a list to display a dynamic number of custom views.我使用列表来显示动态数量的自定义视图。 See code.见代码。

 @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)
    }

After that, I had this list.在那之后,我有了这个清单。

在此处输入图像描述

After that I modified the list like this.之后,我像这样修改了列表。

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

The bottom of the list has now changed to clear, but the cells are still white.列表底部现在已更改为清除,但单元格仍然是白色的。

在此处输入图像描述

If I edit it like this:如果我这样编辑它:

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

The list looks like this:该列表如下所示:

在此处输入图像描述

Hope someone can help!希望有人能帮忙!

Change background color of a view by simply adding.background property只需添加 .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)
}

Or you can use listRowBackground property或者您可以使用 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM