gpt4 book ai didi

if-statement - 根据数组值在 SwiftUI 中有条件地文本

转载 作者:行者123 更新时间:2023-12-02 00:13:13 25 4
gpt4 key购买 nike

我想制作占位符自定义样式,所以我尝试使用 SwiftUI. How to change the placeholder color of the TextField? 中的 Mojtaba Hosseini 方法

if text.isEmpty {
Text("Placeholder")
.foregroundColor(.red)
}

但在我的例子中,我使用带有数组的 foreach 来制作文本字段和显示的列表,或者不显示用于模拟自定义占位符的文本。

          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, ListeJoueurView> count (3) != 其初始计数 (2)。 ForEach(_:content:) 应该只用于常量 数据。而是使数据符合 Identifiable 或使用 ForEach(_:id:content:) 并提供明确的 id!

谁能教教我?

最佳答案

长话短说

在每个 ForEach 之外,您需要一个 VStackHStackList 等。

已更新

对于问题的第二部分,您需要更改 ForEach 以包含 id 参数:

ForEach(self.ListeJoueurs[EquipName].indices, id: \.self)

如果数据不是常量并且元素的数量可能会发生变化,您需要包含 id:\.self 以便 SwiftUI 知道在哪里插入新 View 。

例子

下面是一些演示嵌套 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)
}
}
}
}

解释

这是Apple's documentation关于 ForEach 的说法:

A structure that computes views on demand from an underlying collection of of [sic] identified data.

类似的东西

ForEach(0..2, id: \.self) { number in
Text(number.description)
}

实际上只是

的简写
Text("0")
Text("1")
Text("2")

所以你的 ForEach 正在制作一堆 View ,但是这种声明 View 的语法只在 VStack、HStack、List、Group 等 View 中有效。技术原因是因为这些 View 有一个看起来像

init(..., @ViewBuilder content: () -> Content)

@ViewBuilder 做了一些允许这种独特语法的魔法。

关于if-statement - 根据数组值在 SwiftUI 中有条件地文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com