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 BSTableViewReorderfor every file when you need to use it.
you may also use:
@import BSTableViewReorderwithin 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
BSTableViewReorderas a subclass of yourUITableViewin Interface Builder.Implement
tableView:moveRowAtIndexPath:toIndexPathof theUITableViewDataSourceprotocol:
func tableView(tableView:UITableView, moveRowAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath){letobj=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{returnself.tableView.adaptedNumberOfRowsInSection(section, withNumberOfRows:data[section].count)}func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell{letcell= tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath)as!BSTableViewCellletadaptedIndexPath=self.tableView.adaptedIndexPathForRowAtIndexPath(indexPath)
cell.label?.text =data[adaptedIndexPath.section][adaptedIndexPath.row]return cell
}#####Customize the way you work with BSTableViewReorder using BSTableViewReorderDelegate:
@objcpublicprotocolBSTableViewReorderDelegate:class,UITableViewDelegate{@objcoptionalvartableViewCanReorder:Bool{getset}@objcoptionalvarsnapshotOpacity:Float{getset}@objcoptionalfunc tableViewDidStartLongPress(gestureRecognizer:UILongPressGestureRecognizer)@objcoptionalfunc tableViewDidEndLongPress(gestureRecognizer:UILongPressGestureRecognizer)@objcoptionalfunc transformForSnapshotOfReorderingCell(atIndexPath indexPath:IndexPath)->CATransform3D}First thing you need to do is assign your delegate object to reorderDelegate property. Below is example usage:
vartableViewCanReorder:true //you can modify this value at runtime.
varsnapshotOpacity=0.7 //you can modify this value at runtime
@IBOutletvartableView:BSTableViewReorder!overridefunc viewDidLoad(){
super.viewDidLoad()
tableView.reorderDelegate =self}func transformForSnapshotOfReorderingCellAtIndexPath(indexPath:NSIndexPath)->CATransform3D{vartransform= 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, bartekss2@icloud.com
BSTableViewReorder is available under the MIT license. See the LICENSE file for more info.
