mmick66 / kddraganddropcollectionview Goto Github PK
View Code? Open in Web Editor NEWThis component allows for the transfer of data items between collection views through drag and drop
License: MIT License
This component allows for the transfer of data items between collection views through drag and drop
License: MIT License
Hi There, Thanks for awesome extension and your great work!
I am implementing this for my UITableView
where each cell is a CollectionView, however KDDragAndDropManager
only works for the initial visible cells, If I scroll and load new cells, those collectionViews will not be droppable.
Can you suggest a place that I can look deeper to address the issue.
Cheers
Hello! At first, I would say thank you for that awesome extension, very helpful for my project. I downloaded your examples, and probably found a bug, and I even tried to fix that, but because I don't have a lot of experience, couldn't do that. If in collectionView function just remove "if draggingPathOfCellBeingDragged.item == indexPath.item {cell.hidden = true}" it doesn't have this bug, but looks not so good.
Problem:
When a try to drag and drop easy, everything looks good, but when I do it so much faster and do like drop outside, cell exist in new view, but hidden. For more understanding I record a video for you. Please watch this video on https://www.dropbox.com/s/rtq0pj1771v8txg/kddragfail.mov?dl=0
I am unsure if this is an iOS 13 issue, a modal view controller presentation issue, or an issue with the source code, but the collection view cells go crazy when I am dragging and dropping:
This is quite frustrating for the user because cells are not dropped in the place the pan gesture ends and it takes multiple tries to achieve the desired result and sometimes the desired result is impossible to achieve.
Hello Michael, It would be great if you could help me with adding new function in your project. I need to place a UIButton inside the CollectionViewCell, that will remove self Cell with existing animation; same like when dragging and dropping items.
Now I realized that just by placing function in UITableViewController class and UIButton with "addTarget", but It hasn't your animation.
None of them seem to be accessible when the lib is installed via Cocoapods. Am I missing something?
I have UILabel in the collectionViewCell and each label have one character. Now user can move these labels and form a word or user drop these labels to another collectionview. Framework work behaving weird if there is same characters in the single word eg. offence
whenever I tried to move f
and e
the another same char also moves along it.
Is it possible to make the layout in vertical order, where cell will be created by one by one in rows.
Mine target to make it in cell should place in vertical order, with 1 column and independent of cell size.
I configured my collection view cells to calculate their height by overriding UICollectionViewCell.preferredLayoutAttributesFitting
.
When I do this, dragging appears to break in a weird way. When I first drag an item from one collection view to another, the items in the collection view I'm dragging to all move down to make room for the cell I'm dragging, even though the cell I'm dragging barely even entered the other collection view. After about a second, the cells in the collection view I'm dragging to all re-arrange and go back to normal. At that point, I can drag the new cell into it's position.
Any ideas on what this may be? I've spent the last two days trying to figure it out...
It seems to be something in the KDDragAndDropCollectionView.didMoveItem
function.
Thanks.
Instead of moving I would like to use this for copying items. Will it be possible to modify this to achieve that?
Hi,there. When I dragged an item from sectionA, (finger keeping touch) ,and used another finger click sectionB's item, it will cause UI wrong. I think it should set collectionView GestureRecognizer disable when user begin dragging item.
Look forward to your reply.
Is there a method for the "drop" action? When I drag one of the cells out of one collection view to the other, the delete and insert method is called. Is there a method that is fired when I release from my long press and drop the cell in its new location?
Only dataItemForIndexPath KDDragAndDropCollectionViewDataSource is called when tapped on cell not other method is calling when i try to drag.
as title thx
hello
i have two collectionview
user can drag from one collectionview to Second collectionview but how to prevent from second collectionview to one collectionview
thanks
Hey @mmick66, your lib is awesome but your README lacks an installation section
I created this iOS README template to help you out π
I'd be cool to also add Cocoapods support. I've done this implementation myself last week for my library. Just follow this post and this checklist.
Please fix these issues before we can approve the AwesomeiOS PR
Looking for a way to reload all the collectionviews, retriggering all the datasource delegate methods.
Thanks!
I am not able to detect cell Tap of collectionView. Any Suggestion what I can do ?
While Drag the cell I can't create a new section.
For some reason when I implemented the exact same files into my own project, I am getting this issue whenever I drag a cell out of a collection view:
βInvalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (21) must be equal to the number of items contained in that section before the update (21), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).β
I set up the storyboard the exact same way as the example project, but I can't seem to figure out this issue. Thanks, Neil
I've recently discovered this library and would really love to work with it. I downloaded the example project and it works like a charm!
Unfortunately, I can neither use storyboards for my case (as the number of collection views needs to be adjusted to the number of arrays in my data) nor do I have a lot of time due to my next semester starting next week (and therefore being a little stressed).
(Did I miss some non-storyboard code for the collection views? If so, please let me know.)
Anyway, for 2 days now, I tried to implement this library such that it takes my data and creates as many collection views as arrays in my data (= array of arrays of ProgrammaticallyDragAndDropItem
s). I've tried a lot at this point but can not at all put my finger on why I am constantly getting the same error (EXC_BAD_ACCESS at performBatchUpdates
in KDDragAndDropCollectionView.swift
) whenever I try to drag one of the cells to another position - the app crashes immediately. If nothing is moved and the cell remains at its previous position, however, nothing happens, i.e. the recognition of the right cell (and the item's index in the data if I print it in the console) works fine.
This is what invariably happens:
I know you probably don't have a lot of time but I'd really appreciate if you could take a glance at my code and tell me what I should try or what you think could be the problem since I'm really frustrated at this point. It's very close to the code in the example. I'll post it below and focus on the important things first. I hope I am not missing something obvious. Please bear with me π¬
Here is the extension for the collection views first (below is the class of the controller & the item):
// cvIndex = index of the collection view in the array of KDDragAndDropCollectionViews (instead of tags)
extension ProgrammaticallyDragAndDropController: KDDragAndDropCollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard dragAndDropItems.count != 0, let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }) else { return 0 }
return dragAndDropItems[cvIndex].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }), let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kdCollectionViewsIDs[cvIndex], for: indexPath) as? ProgrammaticallyDragAndDropItemCell else {
fatalError("fatal: cell for item at index path")
}
cell.programmaticallyDragAndDropItem = dragAndDropItems[cvIndex][indexPath.item]
cell.isHidden = false
if let kdCollectionView = collectionView as? KDDragAndDropCollectionView {
if let draggingPathOfCellBeingDragged = kdCollectionView.draggingPathOfCellBeingDragged {
cell.isHidden = draggingPathOfCellBeingDragged.item == indexPath.item
}
}
return cell
}
// added by KDDragAndDropCollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, dataItemForIndexPath indexPath: IndexPath) -> AnyObject {
guard let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }) else { fatalError("fatal: data item for index path") }
print("data item for cvindex indexpath", cvIndex, indexPath.item)
return dragAndDropItems[cvIndex][indexPath.item]
}
func collectionView(_ collectionView: UICollectionView, insertDataItem dataItem: AnyObject, atIndexPath indexPath: IndexPath) {
if let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }), let dragAndDropItem = dataItem as? ProgrammaticallyDragAndDropItem {
dragAndDropItems[cvIndex].insert(dragAndDropItem, at: indexPath.item)
}
}
func collectionView(_ collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath: IndexPath) {
if let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }) {
dragAndDropItems[cvIndex].remove(at: indexPath.item)
}
}
func collectionView(_ collectionView: UICollectionView, moveDataItemFromIndexPath from: IndexPath, toIndexPath to: IndexPath) {
if let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }) {
let dragAndDropItem = dragAndDropItems[cvIndex][from.item]
dragAndDropItems[cvIndex].remove(at: from.item)
dragAndDropItems[cvIndex].insert(dragAndDropItem, at: to.item)
}
}
func collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -> IndexPath? {
if let candidate = dataItem as? ProgrammaticallyDragAndDropItem, let cvIndex = kdCollectionViews.firstIndex(where: { $0 == collectionView }) {
for (index, item) in dragAndDropItems[cvIndex].enumerated() {
if candidate != item { continue }
return IndexPath(item: index, section: 0)
}
}
return nil
}
func collectionView(_ collectionView: UICollectionView, cellIsDroppableAtIndexPath indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, cellIsDraggableAtIndexPath indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, stylingRepresentationView: UIView) -> UIView? {
return nil
}
}
This is the class (which probably isn't especially important):
import UIKit
import Stevia
import KDDragAndDropCollectionViews
class ProgrammaticallyDragAndDropController: UIViewController {
var kdManager: KDDragAndDropManager?
var dragAndDropItems = [
[ProgrammaticallyDragAndDropItem(id: "id1", name: "name1", belongsTo: "group1"), ProgrammaticallyDragAndDropItem(id: "id2", name: "name2", belongsTo: "group1"), ProgrammaticallyDragAndDropItem(id: "id3", name: "name3", belongsTo: "group1")],
[ProgrammaticallyDragAndDropItem(id: "id4", name: "name4", belongsTo: "group2"), ProgrammaticallyDragAndDropItem(id: "id5", name: "name5", belongsTo: "group2"), ProgrammaticallyDragAndDropItem(id: "id6", name: "name6", belongsTo: "group2")],
[ProgrammaticallyDragAndDropItem(id: "id7", name: "name7", belongsTo: "group3"), ProgrammaticallyDragAndDropItem(id: "id8", name: "name8", belongsTo: "group3"), ProgrammaticallyDragAndDropItem(id: "id9", name: "name9", belongsTo: "group3")]
]
var kdCollectionViews = [KDDragAndDropCollectionView]()
var kdCollectionViewsIDs = [String]()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
kdManager = KDDragAndDropManager(canvas: view, collectionViews: kdCollectionViews)
}
private func setupViews() {
// fixme: for loop -> number of groups = number of collection views
// in this case 3 collection views (created below)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let testCVs = [KDDragAndDropCollectionView(frame: .zero, collectionViewLayout: layout), KDDragAndDropCollectionView(frame: .zero, collectionViewLayout: layout), KDDragAndDropCollectionView(frame: .zero, collectionViewLayout: layout)]
let testCVIDs = ["cellID0", "cellID1", "cellID2"]
kdCollectionViews.append(contentsOf: testCVs)
kdCollectionViewsIDs.append(contentsOf: testCVIDs)
let safeView = UIView()
view.sv(
safeView.sv(
kdCollectionViews
)
)
safeView.fillHorizontally()
safeView.Top == view.safeAreaLayoutGuide.Top
safeView.Bottom == view.safeAreaLayoutGuide.Bottom
for (index, kdCollectionView) in kdCollectionViews.enumerated() {
kdCollectionView.dataSource = self
kdCollectionView.delegate = self
kdCollectionView.register(ProgrammaticallyDragAndDropItemCell.self, forCellWithReuseIdentifier: kdCollectionViewsIDs[index])
// layout with Stevia (just sugar, it's the same as constraints)
kdCollectionView.fillHorizontally()
kdCollectionView.height(100)
kdCollectionView.backgroundColor = .red
// attach to top of safe view (safe area insets) if first cv
if index == 0 {
kdCollectionView.Top == safeView.Top
// otherwise attach 100 points below previous cv
} else {
kdCollectionView.Top == kdCollectionViews[index - 1].Bottom + 100
}
}
}
}
The cell is pretty much a UICollectionViewCell with another background color right now.
This is my item class:
class ProgrammaticallyDragAndDropItem: Equatable {
var id: String
var name: String
var belongsTo: String
init(id: String, name: String, belongsTo: String) {
self.id = id
self.name = name
self.belongsTo = belongsTo
}
static func == (lhs: ProgrammaticallyDragAndDropItem, rhs: ProgrammaticallyDragAndDropItem) -> Bool {
return lhs.id == rhs.id && lhs.name == rhs.name && lhs.belongsTo == rhs.belongsTo
}
}
Sometimes the image of the cell gets stuck in the collection view and will only go away when the app is restarted.
Thanks, Neil
I want to stop animation, animation like create new space for card , how can we do this
Dragable is working but it is not working on dropable... can u please modify your example
TIA.
Is ti possible to change access modifier for class KDDragAndDropCollectionView from 'public' to 'open', to provide the possibility of inheritance from KDDragAndDropCollectionView?
Hi,
I used your library. Great library by the way. I found a bug. For example i have array of [A,B, C, D, A] when i swap A to A, the whole cell moves. Is there any solution for this? i just wanted to swap A to A. i am working on a word puzzle.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.