{"id":2876,"url":"https://github.com/mmick66/KDDragAndDropCollectionView","last_synced_at":"2025-08-06T16:31:43.806Z","repository":{"id":56917121,"uuid":"41495284","full_name":"mmick66/KDDragAndDropCollectionView","owner":"mmick66","description":"This component allows for the transfer of data items between collection views through drag and drop","archived":false,"fork":false,"pushed_at":"2020-04-21T06:19:34.000Z","size":467,"stargazers_count":528,"open_issues_count":1,"forks_count":80,"subscribers_count":22,"default_branch":"master","last_synced_at":"2024-12-06T13:10:12.735Z","etag":null,"topics":["collection-view","collectionview","drag","drag-and-drop","drop","swift","uicollectionview"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mmick66.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-27T15:31:52.000Z","updated_at":"2024-11-21T07:19:01.000Z","dependencies_parsed_at":"2022-08-20T21:20:27.975Z","dependency_job_id":null,"html_url":"https://github.com/mmick66/KDDragAndDropCollectionView","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmick66%2FKDDragAndDropCollectionView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmick66%2FKDDragAndDropCollectionView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmick66%2FKDDragAndDropCollectionView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmick66%2FKDDragAndDropCollectionView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mmick66","download_url":"https://codeload.github.com/mmick66/KDDragAndDropCollectionView/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228923722,"owners_count":17992568,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["collection-view","collectionview","drag","drag-and-drop","drop","swift","uicollectionview"],"created_at":"2024-01-05T20:16:25.199Z","updated_at":"2024-12-09T16:31:10.488Z","avatar_url":"https://github.com/mmick66.png","language":"Swift","readme":"# Drag and Drop Collection Views \r\n\r\nWritten for Swift 4.0, it is an implementation of Dragging and Dropping data across multiple UICollectionViews. \r\n\r\n![Drag and Drop Illustration](https://github.com/mmick66/KDDragAndDropCollectionView/blob/master/Resources/header.png?raw=true \"Drag and Drop\")\r\n\r\nTry it on [Appetize.io!](https://appetize.io/embed/exaf5fdj5auryhu174ta69t1gm?device=iphone5s\u0026scale=75\u0026orientation=portrait\u0026osVersion=9.3)\r\n\r\n[![Language](https://img.shields.io/badge/Swift-4.0-orange.svg?style=flat)](https://swift.org)\r\n[![Licence](https://img.shields.io/dub/l/vibe-d.svg?maxAge=2592000)](https://opensource.org/licenses/MIT)\r\n[![CocoaPods](https://img.shields.io/cocoapods/v/KDCalendar.svg?style=flat)](https://cocoapods.org/pods/KDDragAndDropCollectionViews)\r\n[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/vsouza/awesome-ios)\r\n\r\n## Requirements\r\n\r\n* iOS 8.0+\r\n* XCode 9.0+\r\n* Swift 4.0 +\r\n\r\n## Installation\r\n\r\n#### Cocoa Pods\r\n```\r\npod 'KDDragAndDropCollectionViews', '~\u003e 1.5.2'\r\n```\r\n#### Manual\r\n\r\nAdd the files in `Classes/` to your project.\r\n\r\n## Quick Guide\r\n\r\nMake the UICollectionView of interest a `KDDragAndDropCollectionView`\r\n\r\n![XCode Interface Builder Screen](https://github.com/mmick66/KDDragAndDropCollectionView/blob/master/Resources/Screenshot.Installation.png?raw=true)\r\n\r\nThen set a class as dataSource implementing the `KDDragAndDropCollectionViewDataSource` protocol.\r\n\r\n```Swift\r\nclass ViewController: UIViewController, KDDragAndDropCollectionViewDataSource {\r\n\r\n    @IBOutlet weak var firstCollectionView: KDDragAndDropCollectionView!\r\n    @IBOutlet weak var secondCollectionView: KDDragAndDropCollectionView!\r\n    @IBOutlet weak var thirdCollectionView: KDDragAndDropCollectionView!\r\n    \r\n    var data : [[DataItem]] = [[DataItem]]() // just for this example\r\n    \r\n    var dragAndDropManager : KDDragAndDropManager?\r\n    \r\n    override func viewDidLoad() {\r\n        let all = [firstCollectionView, secondCollectionView, thirdCollectionView]\r\n        self.dragAndDropManager = KDDragAndDropManager(canvas: self.view, collectionViews: all)\r\n    }\r\n}\r\n```\r\n\r\nThe only responsibility of the user code is to manage the data that the collection view cells are representing. The data source of the collection views must implement the `KDDragAndDropCollectionViewDataSource` protocol.\r\n\r\nIn the example we have 3 UICollectionViews distinguishable by their tags (bad practice, I know... but it's only an example ;-) and a data array holding 3 arrays respectively. In a case like this, an implementation of the above could be:\r\n\r\n```Swift\r\nfunc collectionView(collectionView: UICollectionView, dataItemForIndexPath indexPath: NSIndexPath) -\u003e AnyObject {\r\n    return data[collectionView.tag][indexPath.item]\r\n}\r\n\r\nfunc collectionView(collectionView: UICollectionView, insertDataItem dataItem : AnyObject, atIndexPath indexPath: NSIndexPath) -\u003e Void {\r\n    if let di = dataItem as? DataItem {\r\n        data[collectionView.tag].insert(di, atIndex: indexPath.item)\r\n    }\r\n}\r\n\r\nfunc collectionView(collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath : NSIndexPath) -\u003e Void {\r\n    data[collectionView.tag].removeAtIndex(indexPath.item)\r\n}\r\n\r\nfunc collectionView(collectionView: UICollectionView, moveDataItemFromIndexPath from: NSIndexPath, toIndexPath to : NSIndexPath) -\u003e Void {\r\n    let fromDataItem: DataItem = data[collectionView.tag][from.item]\r\n    data[collectionView.tag].removeAtIndex(from.item)\r\n    data[collectionView.tag].insert(fromDataItem, atIndex: to.item)    \r\n}\r\n\r\nfunc collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -\u003e IndexPath? {\r\n\r\n    guard let candidate = dataItem as? DataItem else { return nil }\r\n    \r\n    for (i,item) in data[collectionView.tag].enumerated() {\r\n        if candidate != item { continue }\r\n        return IndexPath(item: i, section: 0)\r\n    }\r\n    return nil\r\n}\r\n```\r\n\r\n## Advanced Use\r\n\r\n#### Prevent specific Items from being Dragged and/or Dropped\r\n\r\nFor a finer tuning on what items are draggable and which ones are not we can implement the following function from the `KDDragAndDropCollectionViewDataSource` protocol\r\n\r\n```Swift\r\nfunc collectionView(_ collectionView: UICollectionView, cellIsDraggableAtIndexPath indexPath: IndexPath) -\u003e Bool {\r\n    return indexPath.row % 2 == 0\r\n}\r\n```\r\n\r\n\r\n#### Data Items and Equatable\r\n\r\nIn the example code included in this project, I have created a `DataItem` class to represent the data displayed by the collection view.\r\n\r\n```Swift\r\nclass DataItem : Equatable {\r\n    var indexes: String\r\n    var colour: UIColor\r\n    init(indexes: String, colour: UIColor = UIColor.clear) {\r\n        self.indexes    = indexes\r\n        self.colour     = colour\r\n    }\r\n    static func ==(lhs: DataItem, rhs: DataItem) -\u003e Bool {\r\n        return lhs.indexes == rhs.indexes \u0026\u0026 lhs.colour == rhs.colour\r\n    }\r\n}\r\n```\r\n\r\nIn the course of development you will be making your own types that must comform to the `Equatable` protocol as above. Each data item must be **uniquely idenfyiable** so be careful when creating cells that can have duplicate display values as for example a [\"Scrabble\"](https://en.wikipedia.org/wiki/Scrabble) type game where the same letter appears more than once. In cases like these, a simple identifier will do to implement the equality.\r\n","funding_links":[],"categories":["UI","Libs","UI [🔝](#readme)"],"sub_categories":["Table View / Collection View","UI","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmick66%2FKDDragAndDropCollectionView","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmmick66%2FKDDragAndDropCollectionView","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmick66%2FKDDragAndDropCollectionView/lists"}