1

我有一个简单的嵌套列表,它使用父项生成DisclosureGroup ,但是当我点击子节点中的Button时,它被多次调用,其中也包括子视图初始化。

struct TopicView: View {
    @ObservedObject private var levelsListVM = LevelsViewModel()
    @State var isActive = false
    @State var levelId : Int = 0
    @State var topicId : Int = 0
var body: some View {
      
            List {
                
                ForEach(self.levelsListVM.levelsWithTopics, id:\.level ) { level in
                    DisclosureGroup(
                        isExpanded: .constant(!level.isLock),
                        content: {
                            VStack {
                                ForEach(level.topics, id:\.title ) { topic in
                                    
                                    Button (action: {
                                        self.levelId = level.level
                                        self.topicId = topic.id
                                        self.isActive = true
                                    }, label: {
                                        ImageDisplay(imageURL: topic.imageUrl).clipShape(Circle())
                                            .background(Color.blue)
                                            .frame(width: 50, height: 50)
                                    }).fullScreenCover(isPresented: self.$isActive, content: {
                                                                                             GameTwo(level: level.level, topicId: topic.id, practiceMode: true)
                                                                                     })
                                }
                                
                            }
                            
                        },
                        label: {
                            HStack {
                                ImageDisplay(imageURL: level.levelImageUri).clipShape(Circle())
                                    .background(Color.blue)
                                    .frame(width: 100, height: 100, alignment: .center)
                                
                                HStack {
                                    VStack {
                                        Text(level.levelName)
                                        Text("Level  0 / 2 / 3").font(.subheadline)
                                    }
                                }
                                
  
                            }
                            
                            
                        }
                        
                    )
                    
                }
            }
           
            .navigationTitle("Demo")
            
        
    }
}

我怎样才能避免这种行为?

仅供参考:我正在使用ObservedObjectMVVM模式来传递数据。

4

0 回答 0