要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install
。
DragIt 通过 CocoaPods 可用。要安装它,只需将以下行添加到您的 Podfile 中:
pod "DragIt"
使用 DragAndDropTableView
或 DragAndDropTableViewController
class MyController {
var dragItTable: DragAndDropTableView
// DO YOUR STUFF
}
或者您可以继承它
class MyController: DragAndDropTableView {
// DO YOUR STUFF
}
但如果您不使用我的类,您可以创建自己的类并添加类似的东西
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)
}
}
}
DragAndDropTableDelegate
有四个方法
@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
}
只有第一个是必需的.
move()
该函数在移动单元格时调用,指示单元格从哪个位置移动到哪个位置。
如您在 示例 中所见,您可以使用这种方式移动您的项目
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
返回一个布尔值,以指示在 atIndex
是否可以移动单元格。
默认为 true
canMove()
func canMove(fromSection: Int, toSection: Int) -> Bool
返回一个布尔值,指示单元格是否可以从一个部分(fromSection
)移动到另一个部分(toSection
)。
默认为 false
backgroundColor()
func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor
用于指示拖动单元格时单元格的背景颜色。
默认使用所选颜色为单元格的背景色。
DragIt 由 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。