0

我遇到了关于图像上的点击操作的问题。我有一个简单的项目列表,每行有 4 个按钮。每行有 4 个图像,每个图像上都有一个链接的 onTapGesture 动作。我第一次加载列表并且一切正常,但是当我尝试在列表中添加新项目(通过在导航栏上添加链接)时,会创建一个新行但是如果我尝试点击,点击操作不起作用我添加的最后一行中的 4 个图像之一。例如,向上或向下箭头需要向上或向下移动一行。添加新行后,新行上的向上/向下箭头不起作用,但如果我点击最后一行 -1 箭头,则两行开关和插入行上的图像点击操作开始起作用。

subCategorie 列表来自另一个视图。

    struct ExpandedCategoriaView : View {
        @Binding var subCategorie : [DisegnoCategoria]
    ......
                    List{
                        
                        ForEach(self.subCategorie, id: \.id){
                            disegno in
                            HStack{
                                if(disegno.cancellato == 1 ){
                                    Text(LocalizedStringKey(disegno.label))
                                        .padding(.leading,10)
                                        .foregroundColor(.red)
                                    Spacer()
                                    Image(systemName: "clock.arrow.circlepath")
                                        .padding(EdgeInsets(top: 5 , leading: 5, bottom: 5, trailing: 5))
                                        .overlay(
                                            RoundedRectangle(cornerRadius: 8)
                                                .stroke(Color.red, lineWidth: 1))
                                        .imageScale(.medium)
                                        .onTapGesture {
                                            DBHelper.dbHelperInstance.restoreLogicDisegnoCategoriaById(idDisegnoCategoria: disegno.id)
                                            subCategorie = getDisegnoCategorieByCategoria()
    
                                        }
                                }
                                else{
                                    
                                    if(disegno.editable == 0)
                                    {
                                        Text(LocalizedStringKey(disegno.label))
                                            .padding(.leading,10)
                                            .foregroundColor(.blue)
                                        
                                        Spacer()
                                    }
                                    else
                                    {
                                        Text(LocalizedStringKey(disegno.label))
                                            .padding(.leading,10)
                                            .foregroundColor(.green)
                                        Spacer()
                                        
                                        HStack{
                                            
                                            
                                            Image(systemName: "arrow.down")
                                                .padding(EdgeInsets(top: 5 , leading: 5, bottom: 5, trailing: 5))
                                                .overlay(
                                                    RoundedRectangle(cornerRadius: 8)
                                                        .stroke(Color.orange, lineWidth: 1))
                                                .imageScale(.medium)
                                                .onTapGesture {
                                    
                                                    goDown(disegnoCategoria: disegno)
                                                    
                                                }
                                                .padding(.trailing,25)
                                          
                                            
                                            Button(action: {
                                                goUp(disegnoCategoria: disegno)
                                                print("spinto bottone")
                                                
                                            })
                                            {
                                                Image(systemName: "arrow.up")
                                                     .padding(EdgeInsets(top: 5 , leading: 5, bottom: 5, trailing: 5))
                                                     .overlay(
                                                         RoundedRectangle(cornerRadius: 8)
                                                             .stroke(Color.orange, lineWidth: 1))
                                                     .imageScale(.medium)
                                                     .padding(.trailing,25)
    
                                                    
                                            }
                                            .buttonStyle(BorderlessButtonStyle())
                                            
                                            //.buttonStyle(PlainButtonStyle())
                                  
                                            
                                            Image(systemName: "pencil")
                                                .padding(EdgeInsets(top: 5 , leading: 5, bottom: 5, trailing: 5))
                                                .overlay(
                                                    RoundedRectangle(cornerRadius: 8)
                                                        .stroke(Color.yellow, lineWidth: 1))
                                                .imageScale(.medium)
                                                .onTapGesture {
                                                    activeSheetDisegnoCategorie = .update
                                                    self.selectedDisegno = disegno
                                                    
                                                }
                                            
                                            Image(systemName: "trash.fill")
                                                .padding(EdgeInsets(top: 5 , leading: 5, bottom: 5, trailing: 5))
                                                .overlay(
                                                    RoundedRectangle(cornerRadius: 8)
                                                        .stroke(Color.red, lineWidth: 1))
                                                .imageScale(.medium)
                                                .onTapGesture {
                                                    DBHelper.dbHelperInstance.deleteLogicDisegnoCategoriaById(idDisegnoCategoria: disegno.id)
                                                    subCategorie = getDisegnoCategorieByCategoria()
    
                                                }
                                                .padding(.leading,25)
                                            
                                        }
                                        
                                    }
                                }
                              
                            }  
                        }
                    }
      .navigationBarTitle(Text(LocalizedStringKey("categoria_layout_expand_titolo")),displayMode: .inline)
                .navigationBarItems(trailing: Button(action: {
                    
                    activeSheetDisegnoCategorie = .add
                }, label:  {
                    Text(LocalizedStringKey("add"))
                }))
                
                .sheet(item: $activeSheetDisegnoCategorie, onDismiss: {
                    self.subCategorie = getDisegnoCategorieByCategoria()
            
                   
                  
                }) {
                    item in
                    switch item {
                    case .add :
                        AddDisegnoCategoriaView(activeSheetDisegnoCategoria: $activeSheetDisegnoCategorie,listaDisegnoCategorie: $sottocate.listaDisegnoCategorie,toRefresh: $updater, categoriaExpanded: categoriaToExpand,  nuovoDisegnoCategoria: {
                            disegnoCategoria in
                            DBHelper.dbHelperInstance.insertDisegnoCategoriaObj(disegnoCategoria: disegnoCategoria)
                            subCategorie = getDisegnoCategorieByCategoria()
                            sottocate.listaDisegnoCategorie = getDisegnoCategorieByCategoria()
                                                
                        })
                       
    
    
                    }
                }
                
            }
            .onAppear(){
       
                getListaSubCategorie()
                getListaSubCategorieProva()
               
            }
    }



    class DisegnoCategoria : Identifiable {
        var id : Int
        var idCategoria: Int
        var ordine : Int
        var tipo : Int
        var label : String
        var editable : Int
        var cancellato : Int
        var obbligatorio : Int
        
        init(id: Int, idCategoria: Int, ordine: Int, tipo: Int, label: String, editable: Int, cancellato: Int, obbligatorio: Int) {
            
            self.id = id
            self.idCategoria=idCategoria
            self.ordine=ordine
            self.tipo = tipo
            self.label=label
            self.editable=editable
            self.cancellato=cancellato
            self.obbligatorio=obbligatorio
    
        }
        
        init() {
            self.id = -1
            self.idCategoria = -1
            self.ordine = -1
            self.tipo = -1
            self.label = ""
            self.editable = -1
            self.cancellato = -1
            self.obbligatorio = -1  
        }
       }

插入 2 行并重新加载视图 [1] 后: https ://i.stack.imgur.com/DL0No.png

点击足球向上箭头 [2] 后: https ://i.stack.imgur.com/pTmXH.png

添加了高尔夫,但如果我尝试点击向上箭头,它不会向上移动 [3]: https ://i.stack.imgur.com/s101V.png

有任何想法吗?谢谢你们

4

0 回答 0