Skip to content

Instantly share code, notes, and snippets.

@filsv
Last active December 26, 2016 10:02
Show Gist options
  • Save filsv/c788a533fa233d8bb30126ccf00ab735 to your computer and use it in GitHub Desktop.
Save filsv/c788a533fa233d8bb30126ccf00ab735 to your computer and use it in GitHub Desktop.
UITableViewCell Reordering
var arrays: [[String: AnyObject]] = [[String: AnyObject]]()
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let dic = self.arrays[sourceIndexPath.section]
if let array = dic["arrays"] as? NSMutableArray
{
let fromIndex = sourceIndexPath.row
let toIndex = destinationIndexPath.row
let fromSection = sourceIndexPath.section
let toSection = destinationIndexPath.section
if fromSection == toSection && fromIndex == toIndex
{
let fromObject = array.object(at: fromIndex)
array.removeObject(at: fromIndex)
array.insert(fromObject, at: toIndex)
if let objToMove = fromObject as? NSMutableDictionary
{
objToMove["position"] = toIndex
}
}
} else {
}
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool
{
return false
}
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath
{
if sourceIndexPath.section == proposedDestinationIndexPath.section
{
return proposedDestinationIndexPath
} else {
return sourceIndexPath
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if let countOfRowsInSection = (arrays[section])["arrays"] as? NSArray
{
return countOfRowsInSection.count
}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment