Latest 0.1.0
Homepage https://github.com/Cantallops/DragIt
License MIT
Platforms ios 8.0
Authors

[![CI Status](http://img.shields.io/travis/Alberto Cantallops/DragIt.svg?style=flat)](https://travis-ci.org/Alberto Cantallops/DragIt)
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

DragIt is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "DragIt"

Usage

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)
    }
  }
}

Delegate

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.

Function 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)
}

Function canMoveCell()

func canMoveCell(atIndex: NSIndexPath) -> Bool

Return a boolean to indicate if the cell at atIndex can be moved.

true by default

Function 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

Function 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.

Next

  • [ ] Add comments in code
  • [ ] Add tests
  • [ ] Add demo gif
  • [ ] Improve animation scroll

License

DragIt is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "DragIt",
    "version": "0.1.0",
    "summary": "DragIt to reorder cells.",
    "description": "Reorder cells of a table simple hold the cell and drag it to new position without editing mode",
    "homepage": "https://github.com/Cantallops/DragIt",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alberto Cantallops": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Cantallops/DragIt.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "DragIt/Classes/**/*"
}

Pin It on Pinterest

Share This