If statement SwiftUI中的有条件文本取决于数组值
我想让占位符自定义样式,所以我尝试在中使用Mojtaba Hosseini的方法 但在我的例子中,我使用一个带有数组的foreach来创建一个Textfield列表,并显示或不显示用于模拟自定义占位符的文本If statement SwiftUI中的有条件文本取决于数组值,if-statement,text,view,conditional-statements,swiftui,If Statement,Text,View,Conditional Statements,Swiftui,我想让占位符自定义样式,所以我尝试在中使用Mojtaba Hosseini的方法 但在我的例子中,我使用一个带有数组的foreach来创建一个Textfield列表,并显示或不显示用于模拟自定义占位符的文本 ForEach(self.ListeEquip.indices, id: \.self) { item in ForEach(self.ListeJoueurs[item].indices, id: \.self){idx in // if self.Liste
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,listJouerView>count(3)!=其初始计数(2)ForEach(uquo:content:)
只能用于常量数据。相反,将数据与可识别的一致,或者使用ForEach(uu:id:content:)并提供明确的id
有人能启发我吗?TL;博士
在每个ForEach
之外,您需要一个VStack
,HStack
,列表
,等等
已更新
对于问题的第二部分,您需要更改ForEach以包含id参数:
ForEach(self.ListeJoueurs[EquipName].indices, id: \.self)
如果数据不是常量,并且元素的数量可能会更改,则需要包含id:\.self
,以便SwiftUI知道在何处插入新视图
例子
下面是一些示例代码,演示了一个嵌套的ForEach。我建立了一个数据模型,与您试图调用它的方式相匹配
struct ContentView: View {
// You can ignore these, since you have your own data model
var ListeEquip: [Int] = Array(1...3)
var ListeJoueurs: [[String]] = []
// Just some random data strings, some of which are empty
init() {
ListeJoueurs = (1...4).map { _ in (1...4).map { _ in Bool.random() ? "Text" : "" } }
}
var body: some View {
VStack { // Added this
ForEach(self.ListeEquip.indices, id: \.self) { item in
VStack { // Added this
ForEach(self.ListeJoueurs[item].indices, id: \.self) { index in
if self.ListeJoueurs[item][index].isEmpty { // If string is blank
Text("Placeholder")
.foregroundColor(.red)
} else { // If string is not blank
Text(self.ListeJoueurs[item][index])
}
}
}.border(Color.black)
}
}
}
}
解释
以下是关于ForEach的看法:
一种结构,根据需要从[sic]标识数据的基础集合中计算视图
大概是
ForEach(0..2, id: \.self) { number in
Text(number.description)
}
它实际上只是
Text("0")
Text("1")
Text("2")
因此,您的ForEach正在生成一组视图,但是这种声明视图的语法仅在诸如VStack、HStack、List、Group等视图中有效。技术原因是这些视图有一个init
init(..., @ViewBuilder content: () -> Content)
而
@ViewBuilder
的神奇之处在于,它允许这种独特的语法。当我试图在ListJouers中添加一个字符串时,我用一个按钮“self.listJouers[item].append(“one more”)”在第一个Foreach中出现了“无法推断通用参数‘数据’”错误,如何修复它?@NicolasM我在我的答案中添加了一个更新。您需要在ForEach中包含id参数。
init(..., @ViewBuilder content: () -> Content)