iOS Development

ios – SwiftUI drag and droppable cells of horizontal listing within a cell of a vertical listing

I am attempting to get a drag an droppable horizontal listing in cell view of a predominant listing which vertical.

right here is my resolution. it doesnt work inside a cell view of an inventory view however it works outdoors of Listing{}. the way to get a horizontal listing to be drag and droppable for rearranging cells inside a cell of a Vertical Listing{}?

that is the primary listing view which present some cells with sections. i eliminated all different besides the one with a cell containg horizontal listing with drag and droppable of this sub listing.

import SwiftUI

struct ChipModel : Codable, Identifiable, Equatable, Hashable {
    let id : UUID
    var isSelected : Bool
    var titleKey : String
    var systemImage : String
}


struct AppView : View {


var myList = [ChipModel(id: UUID(), isSelected: false, titleKey: "Year", systemImage: ""), 
                    ChipModel(id: UUID(), isSelected: false, titleKey: "Month", systemImage: ""),
                    ChipModel(id: UUID(), isSelected: false, titleKey: "Day", systemImage: "")]
    
    var physique: some View {
        VStack {
            NavigationView {
                Listing {
                
                    Part(header: Textual content("doc")) {
                        HStack {
                            ChipView(chips: myList)
                        }
                    }
                }
            }
        }
    }
}




right here is the chipview

import Basis
import SwiftUI
import UniformTypeIdentifiers



struct ChipView : View {
    
    @State var chips : [ChipModel]
    @State var draggedItem: ChipModel?
    
    var physique: some View {
        LazyHStack(spacing : 5) {
            ForEach(chips, id:.id.uuidString) { merchandise in
                Textual content(merchandise.titleKey)
                    .body(minWidth:0, maxWidth:.infinity, minHeight:50)
                    .border(Colour.black).background(Colour.purple)
                    .onDrag({
                        self.draggedItem = merchandise
                        return NSItemProvider(merchandise: nil, typeIdentifier: merchandise.id.uuidString)
                    }) .onDrop(of: [UTType.text], delegate: MyDropDelegate(merchandise: merchandise, gadgets: $chips, draggedItem: $draggedItem))
            }
        }
    }
}

struct MyDropDelegate : DropDelegate {

    let merchandise : ChipModel
    @Binding var gadgets : [ChipModel]
    @Binding var draggedItem : ChipModel?

    func performDrop(data: DropInfo) -> Bool {
        return true
    }

    func dropEntered(data: DropInfo) {
        guard let draggedItem = self.draggedItem else {
            return
        }

        if draggedItem != merchandise {
            let from = gadgets.firstIndex(of: draggedItem)!
            let to = gadgets.firstIndex(of: merchandise)!
            withAnimation(.default) {
                self.gadgets.transfer(fromOffsets: IndexSet(integer: from), toOffset: to > from ? to + 1 : to)
            }
        }
    }
}

Credit: www.ismmailgsm.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button