[英]SwiftUI is it possible to pass range of binding in ForEach?
I'd like to pass a range of an array in a model inside ForEach.我想在 ForEach 内的 model 中传递一个数组范围。
I recreated an example:我重新创建了一个示例:
import SwiftUI
class TheModel: ObservableObject {
@Published var list: [Int] = [1,2,3,4,5,6,7,8,9,10]
}
struct MainView: View {
@StateObject var model = TheModel()
var body: some View {
VStack {
ForEach (0...1, id:\.self) { item in
SubView(subList: $model.list[0..<5]) <-- error if I put a range
}
}
}
}
struct SubView: View {
@Binding var subList: [Int]
var body: some View {
HStack {
ForEach (subList, id:\.self) { item in
Text("\(item)")
}
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
The work around周围的工作
I found is to pass all the list and perform the range inside the subView.我发现是传递所有列表并执行 subView 内的范围。 But I'd like don't do this because the array is very big:但我不想这样做,因为数组非常大:
struct MainView: View {
@StateObject var model = TheModel()
var body: some View {
VStack {
ForEach (0...1, id:\.self) { i in
SubView(subList: $model.list, number: i, dimension: 5)
}
}
}
}
struct SubView: View {
@Binding var subList: [Int]
var number: Int
var dimension: Int
var body: some View {
HStack {
ForEach (subList[number*dimension..<dimension*(number+1)].indices, id:\.self) { idx in
Button(action: {
subList[idx] += 1
print(subList)
}, label: {
Text("num: \(subList[idx])")
})
}
}
}
}
I would pass the model to the subview since it is a class and will be passed by reference and then pass the range as a separate parameter.我会将 model 传递给子视图,因为它是 class 并将通过引用传递,然后将范围作为单独的参数传递。
Here is my new implementation of SubView这是我对 SubView 的新实现
struct SubView: View {
var model: TheModel
var range: Range<Int>
var body: some View {
HStack {
ForEach (model.list[range].indices, id:\.self) { idx in
HStack {
Button(action: {
model.list[idx] += 1
print(model.list)
}, label: {
Text("num: \(model.list[idx])")
})
}
}
}
}
}
Note that I added indices
to the ForEach
header to make sure we access the array using an index and not with a value from the array.请注意,我向ForEach
header 添加了indices
,以确保我们使用索引而不是数组中的值访问数组。
The calling view would then look like调用视图看起来像
var body: some View {
VStack {
SubView(model: model, range: (0..<5))
Text("\(model.list.map(String.init).joined(separator: "-"))")
}
The extra Text
is just there for testing purposes额外的Text
仅用于测试目的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.