Skip to content

Instantly share code, notes, and snippets.

@mxpr
Last active August 29, 2015 14:19
Show Gist options
  • Save mxpr/863f4997bdeee437ac9c to your computer and use it in GitHub Desktop.
Save mxpr/863f4997bdeee437ac9c to your computer and use it in GitHub Desktop.
UITableView Move + Update
func animateChanges()
{
tableView.moveRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0), toIndexPath: NSIndexPath(forRow: 0, inSection: 0))
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic)
}
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'
}
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)
}
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
}
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()
}
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)
}
}
@mxpr
Copy link
Author

mxpr commented Apr 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment