Swiftui 将数组内对象的属性作为绑定传递
我想动态渲染与阵列中对象数量匹配的卡片视图。单卡视图由标题、图像和所选属性组成,其中所选属性应来自类型Swiftui 将数组内对象的属性作为绑定传递,swiftui,Swiftui,我想动态渲染与阵列中对象数量匹配的卡片视图。单卡视图由标题、图像和所选属性组成,其中所选属性应来自类型@Binding。所以我的问题是,如何将数组中特定对象的bool值转换为@Binding struct CardType: Identifiable { var id = UUID() var title: String var image: String var selected: Bool } ScrollView(.horizontal, sho
@Binding
。所以我的问题是,如何将数组中特定对象的bool值转换为@Binding
struct CardType: Identifiable {
var id = UUID()
var title: String
var image: String
var selected: Bool
}
ScrollView(.horizontal, showsIndicators: false, content: {
HStack(spacing: 20) {
ForEach(cardTypes) { cardType in
PartialIcon(title: cardType.title, image: cardType.image, selected: cardType.selected)
}
}
})
这是可能的方法
ScrollView(.horizontal, showsIndicators: false, content: {
HStack(spacing: 20) {
ForEach(Array(cardTypes.enumerated()), id: \.1.id) { index, cardType in
PartialIcon(title: cardType.title, image: cardType.image, selected: self.$cardTypes[index].selected)
}
}
})
这是可能的方法
ScrollView(.horizontal, showsIndicators: false, content: {
HStack(spacing: 20) {
ForEach(Array(cardTypes.enumerated()), id: \.1.id) { index, cardType in
PartialIcon(title: cardType.title, image: cardType.image, selected: self.$cardTypes[index].selected)
}
}
})
谢谢,你的解决方案很好用!但是我想知道使用
CardType
数组并从特定的CardType
中找到正确的索引,解决方案会是什么样子。谢谢,您的解决方案工作得很好!但我想知道,如果使用CardType
数组并从特定的CardType
中找到正确的索引,解决方案会是什么样子。