0

pretty new here, and to coding in general. I actually want to comment on another post, but I don't have enough reputation :(

I'm trying to drag and drop rearrange a lazyVgrid.

I tested this, and indeed it works for a list, but it doesn't work for a LVG.

The hold and drag still works, but it doesn't affect the other times, they stay where they are and you can't place your item anywhere - it just goes back to it's original position.

I had to comment out .onDelete as I need a function for it, but I don't think that's causing the problem, but it still worked as a list.

I've tried a few other solutions on here but can't seem to crack it.

I'm also a little confused about how to reorder items, and have them stay in the new position when I return to the view.

    let book: Book
    let books: [Book]
    
    @State private var selected: Book? = nil
    
    let spacing: CGFloat = 10
    
    var gridItems: [GridItem] {
        [GridItem(.adaptive(minimum: 180))]
    }
    
    var body: some View {
        
            LazyVGrid(columns: gridItems,
                      spacing: spacing
            ) {
                ForEach(books) { item in
                    Group { // Container of a row
                        Button {
                            selected = item
                        } label: {
                            BookTileModel(book: item)
                        }
                    }
                    .onDrag { // mean drag a row container
                        return NSItemProvider()
                    }
                }
//              .delete(perform: deleteItems)
                .onMove(perform: move)
        }
    }
}


struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView(book: books[1], books: books)
    }
}

func move(from source: IndexSet, to destination: Int) {
    books.move(fromOffsets: source, toOffset: destination )
}

struct Book: Hashable, Codable, Identifiable {
    
    var id: Int
    var series: String
    var title: String?
    var issue: String
    var volume: String
    var storyArc: String?
    var description: String?
    var favorite: Bool?
    var read: Int?
    var downloaded: Bool?


    private var imageName: String
    var image: Image {
        Image(imageName)
    }
    
    private var publisherLogo: String
    var logo: Image {
        Image(publisherLogo)
    }
    
}
Benjec83
  • 31
  • 6

0 Answers0