BSTableViewReorder 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "BSTableViewReorder"
如果在您的 podfile 中使用过 use_framework
,只需这样进行处理:
import BSTableViewReorder
每次你需要使用它的时候,对所有文件进行操作。
您还可以使用
@import BSTableViewReorder
在 桥接头文件 中指定,避免在每个需要导入 framework 的文件中导入。
UITableViewAutomaticDimension
只需将 BSTableViewReorder
作为你的 UITableView
的子类添加到 Interface Builder 中。
实现 UITableViewDataSource
协议的 tableView:moveRowAtIndexPath:toIndexPath
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let obj = data[sourceIndexPath.section][sourceIndexPath.row]
data[sourceIndexPath.section].removeAtIndex(sourceIndexPath.row)
data[destinationIndexPath.section].insert(obj, atIndex: destinationIndexPath.row)
}
UITableView
单节的基本使用…无需更多操作…
UITableView
多节的基本使用注意使用 indexPath
的每个方法。由于表格必须始终保持与数据源的同步,您需要关注这一点。BSTableViewReorder
会为您处理这些。您需要做的一切就是采用 UITableViewDelegate
协议方法中的 adaptedIndexPathForRowAtIndexPath:
和 adaptedNumberOfRowsInSection:witNumberOfRows:
这两个方法。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableView.adaptedNumberOfRowsInSection(section, withNumberOfRows: data[section].count)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! BSTableViewCell
let adaptedIndexPath = self.tableView.adaptedIndexPathForRowAtIndexPath(indexPath)
cell.label?.text = data[adaptedIndexPath.section][adaptedIndexPath.row]
return cell
}
BSTableViewReorderDelegate
定制您与 BSTableViewReorder
的协作方式@objc public protocol BSTableViewReorderDelegate: class, UITableViewDelegate {
@objc optional var tableViewCanReorder: Bool { get set }
@objc optional var snapshotOpacity: Float { get set }
@objc optional func tableViewDidStartLongPress(gestureRecognizer: UILongPressGestureRecognizer)
@objc optional func tableViewDidEndLongPress(gestureRecognizer: UILongPressGestureRecognizer)
@objc optional func transformForSnapshotOfReorderingCell(atIndexPath indexPath: IndexPath) -> CATransform3D
}
首先,您需要将您的代理对象分配给 reorderDelegate
属性。以下是一个示例用法:
var tableViewCanReorder: true //you can modify this value at runtime.
var snapshotOpacity = 0.7 //you can modify this value at runtime
@IBOutlet var tableView: BSTableViewReorder!
override func viewDidLoad() {
super.viewDidLoad()
tableView.reorderDelegate = self
}
func transformForSnapshotOfReorderingCellAtIndexPath(indexPath: NSIndexPath) -> CATransform3D {
var transform = CATransform3DIdentity
transform.m34 = CGFloat(1.0 / -1000)
transform = CATransform3DRotate(transform, CGFloat(20*M_PI / 180), 0, 1, 0)
transform = CATransform3DRotate(transform, CGFloat(-15*M_PI / 180), 1, 0, 0)
transform = CATransform3DTranslate(transform, -20, 0, 100)
return transform
}
Bartłomiej Semańczyk, [email protected]
BSTableViewReorder可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。