BSTableViewReorder is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "BSTableViewReorder"
If you used use_framework
in your podfile just simply do:
import BSTableViewReorder
for every file when you need to use it.
you may also use:
@import BSTableViewReorder
within bridging header file and avoid to import framework for every needed file.
##Info
- This library is not working with
UITableViewAutomaticDimension
- entirely written in latest Swift syntax. Works with iOS 9 and Xcode7
##Usage
-
Simply add
BSTableViewReorder
as a subclass of yourUITableView
in Interface Builder. -
Implement
tableView:moveRowAtIndexPath:toIndexPath
of theUITableViewDataSource
protocol:
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)
}
#####Basic usage for UITableView
with one section:
...nothing to do more...
#####Basic usage for UITableView
with multiply sections:
Pay attention of every method using indexPath
. Since table must always be up to date with data source, you need to care about this. BSTableViewReorder
do it for you. All you need to do is to adopt 2 methods: adaptedIndexPathForRowAtIndexPath:
and adaptedNumberOfRowsInSection:witNumberOfRows:
in your UITableViewDelegate
protocol's methods:
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
}
#####Customize the way you work with BSTableViewReorder
using BSTableViewReorderDelegate
:
@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
}
First thing you need to do is assign your delegate object to reorderDelegate
property. Below is example usage:
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 is available under the MIT license. See the LICENSE file for more info.