【问题标题】:Get index value when using ForEach - SwiftUI使用 ForEach 时获取索引值 - SwiftUI
【发布时间】:2020-02-10 22:38:00
【问题描述】:

问题

我正在显示一个数据列表,使用ForEach 语句,当用户与每个视图交互时,大小会发生变化并全屏显示,问题是状态会改变每个视图的大小,而不仅仅是一个选择。试图复制 App Store 主页卡片。

我正在尝试什么

我想也许可以使用索引值

 ForEach(objects.indices, id: \.self) { index in
          ObjectView(home: self.objects[index], showContent: self.$showContent)
 } 

然后可能存储索引值,然后仅在对象索引与存储的索引值匹配时才更改大小。但是,当尝试使用索引时,出现以下错误。

错误:对成员“索引”的引用不明确

完整代码:

struct Object: Identifiable {
    var id = UUID()
    var title: String
    var subtitle: String
    var show = false // Maybe set this to true when tapped then use self.objects[index].show for Fram size, but not sure how to get index or make this work
}

struct AppView: View {

    @EnvironmentObject var session: SessionStore
    @Binding var showContent: Bool


        var body: some View {
        ScrollView(.vertical, showsIndicators: false){
            VStack() {
                VStack(alignment: .center, spacing: 32) {
                    ForEach(objectData, id: \.id) { item in
                        GeometryReader { geometry in

                            ContentView(object: item, showContent: self.$showContent)

                            .offset(y: self.showContent ? -geometry.frame(in: .global).minY : 0)

                        }
                        .frame(height: self.showContent ? screen.height : 464)
                        .frame(maxWidth: self.showContent ? .infinity : screen.width - 40)
                    }
                }.offset(x: 0, y: -80)

            }.padding(.bottom, 300)
            .animation(.spring(response: 0.5, dampingFraction: 0.6, blendDuration: 0))

        }.edgesIgnoringSafeArea(.all)
        }
}


struct ObjectCardView: View {

var object: Object
@Binding var showContent:Bool


    var body: some View {
    VStack(alignment: .leading, spacing: 0) {
        ScrollView(.vertical, showsIndicators: false) {

                    Image(object.image)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(height: self.showContent ? 364 : 284)
                        .clipped()       
                        .onTapGesture {
                            self.session.selectedObject = self.object
                            self.showContent.toggle()
                        }

                        if showContent {
                         VStack {
                              DetailsView(object: Object)
                          }
                        }


          }

     }
 }

PS:我这里有一个不相关的视图问题,即子滚动视图会更改父滚动视图,并且点击子滚动视图实际上会与父滚动视图中的内容进行交互。因此,如果我打开一张卡片并滚动然后点击,AppView 滚动视图也会滚动并打开/关闭其他卡片。

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:
    let arr = [String](repeating: "row", count: 10)
    

    .....

    ForEach(arr.indices) { i in
        Text(self.arr[i] + "\(i)")
    }
    

    为什么? var indices: Range<Int> { get }Range<Int> 的类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 2022-01-23
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多