【发布时间】:2020-01-28 17:09:26
【问题描述】:
我想制作占位符自定义样式,所以我尝试在SwiftUI. How to change the placeholder color of the TextField?中使用 Mojtaba Hosseini 的方法
if text.isEmpty {
Text("Placeholder")
.foregroundColor(.red)
}
但在我的情况下,我使用带有数组的 foreach 来制作 Textfield 和 Display 的列表,或者不使用 Text 来模拟自定义占位符。
ForEach(self.ListeEquip.indices, id: \.self) { item in
ForEach(self.ListeJoueurs[item].indices, id: \.self){idx in
// if self.ListeJoueurs[O][O] work
if self.ListeJoueurs[item][index].isEmpty {
Text("Placeholder")
.foregroundColor(.red)
}
}
}
如何将动态条件与 foreach 一起使用?
现在我有另一个问题:
我有这个代码:
struct EquipView: View {
@State var ListeJoueurs = [
["saoul", "Remi"],
["Paul", "Kevin"]
]
@State var ListeEquip:[String] = [
"Rocket", "sayans"
]
var body: some View {
VStack { // Added this
ForEach(self.ListeEquip.indices) { item in
BulleEquip(EquipName: item, ListeJoueurs: self.$ListeJoueurs, ListeEquip: self.$ListeEquip)
}
}
}
}
struct BulleEquip: View {
var EquipName = 0
@Binding var ListeJoueurs :[[String]]
@Binding var ListeEquip :[String]
var body: some View {
VStack{
VStack{
Text("Équipe \(EquipName+1)")
}
VStack { // Added this
ForEach(self.ListeJoueurs[EquipName].indices) { index in
ListeJoueurView(EquipNamed: self.EquipName, JoueurIndex: index, ListeJoueurs: self.$ListeJoueurs, ListeEquip: self.$ListeEquip)
}
HStack{
Button(action: {
self.ListeJoueurs[self.EquipName].append("") //problem here
}){
Text("button")
}
}
}
}
}
}
struct ListeJoueurView: View {
var EquipNamed = 0
var JoueurIndex = 0
@Binding var ListeJoueurs :[[String]]
@Binding var ListeEquip :[String]
var body: some View {
HStack{
Text("Joueur \(JoueurIndex+1)")
}
}
}
我可以运行应用程序,但是当我单击按钮时,控制台中出现此错误:
ForEach, Int, ListeJueurView> count (3) != 它的初始计数 (2)。 ForEach(_:content:) 应该只用于 constant 数据。而是将数据符合Identifiable 或使用ForEach(_:id:content:) 并提供明确的id!
谁能启发我?
【问题讨论】:
标签: if-statement text view conditional-statements swiftui