Last active
December 26, 2016 10:02
-
-
Save filsv/c788a533fa233d8bb30126ccf00ab735 to your computer and use it in GitHub Desktop.
UITableViewCell Reordering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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