Last active
August 29, 2015 14:19
-
-
Save mxpr/863f4997bdeee437ac9c to your computer and use it in GitHub Desktop.
UITableView Move + Update
This file contains 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
func animateChanges() | |
{ | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic) | |
} |
This file contains 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
func animateChanges() | |
{ | |
tableView.beginUpdates() | |
// ... | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic) | |
// ... | |
tableView.endUpdates() | |
// Results in Exception: | |
// Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell' | |
} | |
This file contains 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
func animateChanges() | |
{ | |
tableView.beginUpdates() | |
// ... | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
// ... | |
tableView.endUpdates() | |
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic) | |
} |
This file contains 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
func animateChanges() | |
{ | |
tableView.beginUpdates() | |
// ... | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
// ... | |
tableView.endUpdates() | |
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0), NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: .Automatic) | |
// Notice the resulting animation, no longer looks like smooth move | |
} |
This file contains 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
func animateChanges() | |
{ | |
CATransaction.begin() | |
CATransaction.setCompletionBlock { | |
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0), NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: .Automatic) | |
} | |
tableView.beginUpdates() | |
// ... | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
// ... | |
tableView.endUpdates() | |
CATransaction.commit() | |
} |
This file contains 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
func animateChanges() | |
{ | |
tableView.beginUpdates() | |
// ... | |
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0)) | |
// ... | |
tableView.endUpdates() | |
tableView.beginUpdates() | |
updateCellAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) | |
updateCellAtIndexPath(NSIndexPath(forRow: 2, inSection: 0)) | |
tableView.endUpdates() | |
} | |
func updateCellAtIndexPath(indexPath: NSIndexPath) | |
{ | |
var cell = tableView.cellForRowAtIndexPath(indexPath) | |
cell.contentView.fadeTransition(0.3) | |
configureCell(cell, atIndexPath:indexPath) | |
} | |
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) | |
{ | |
// ... | |
cell.textLabel!.text = "Updated Text!" | |
// ... | |
} | |
// MARK - UITableViewDataSource | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell | |
// to avoid duplicating code used to update cells | |
configureCell(cell, atIndexPath:indexPath) | |
return cell | |
} | |
// MARK - Extensions | |
// Taken from http://stackoverflow.com/a/27645516 | |
extension UIView { | |
func fadeTransition(duration:CFTimeInterval) | |
{ | |
let animation:CATransition = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut) | |
animation.type = kCATransitionFade | |
animation.duration = duration | |
self.layer.addAnimation(animation, forKey: kCATransitionFade) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.matrixprojects.net/p/uitableview-moves-and-updates