TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Aug 2016 |
SPMSupports SPM | ✗ |
Maintained by Cantallops.
To run the example project, clone the repo, and run pod install
from the Example directory first.
DragIt is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "DragIt"
Use DragAndDropTableView
or DragAndDropTableViewController
class MyController {
var dragItTable: DragAndDropTableView
// DO YOUR STUFF
}
Or you can subclass it
class MyController: DragAndDropTableView {
// DO YOUR STUFF
}
But if you do not use my classes you can create your own class and add something like:
private var dragAndDropFactory: DragAndDropTableFactory?
public var dragAndDropDelegate: DragAndDropTableDelegate? {
get {
return dragAndDropFactory?.delegate
}
set {
if let dAD = dragAndDropFactory {
dAD.delegate = newValue
} else {
let table = self.tableView
dragAndDropFactory = DragAndDropTableFactory(tableView: table, dragAndDropDelegate: newValue)
}
}
}
The DragAndDropTableDelegate
has four methods:
@objc public protocol DragAndDropTableDelegate {
func move(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
optional func canMoveCell(atIndex: NSIndexPath) -> Bool
optional func canMove(fromSection: Int, toSection: Int) -> Bool
optional func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor
}
Only the first one is required.
move()
That function it’s called when move the cell and indicates from which position and to which position the cell had been moved.
As you can see in the Example you can move your item using in this way:
func move(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let fS = fromIndexPath.section
let fR = fromIndexPath.row
let tS = toIndexPath.section
let tR = toIndexPath.row
let color = items[fS][fR]
items[fS].removeAtIndex(fR)
items[tS].insert(color, atIndex: tR)
}
canMoveCell()
func canMoveCell(atIndex: NSIndexPath) -> Bool
Return a boolean to indicate if the cell at atIndex
can be moved.
true
by default
canMove()
func canMove(fromSection: Int, toSection: Int) -> Bool
Return a boolean to indicate if the cell can move from one section (fromSection
) to another (toSection
).
false
by default
backgroundColor()
func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor
Used to indicate which color has to be the background of the cell when drag it.
By default uses a selected color for the cell.
DragIt is available under the MIT license. See the LICENSE file for more info.