Skip to content

Instantly share code, notes, and snippets.

@indragiek
Last active August 16, 2018 07:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indragiek/6837da0d64320d9a4e65 to your computer and use it in GitHub Desktop.
Save indragiek/6837da0d64320d9a4e65 to your computer and use it in GitHub Desktop.
//
// JumpFixTableViewController.swift
//
// Created by Indragie on 1/3/15.
// Copyright (c) 2015 Indragie Karunaratne. All rights reserved.
//
import UIKit
/// Table view controller that implements a workaround for a bug where
/// pushing a view controller onto the navigation stack causes the cells
/// to shift slightly (presumably because its getting the estimated row heights
/// again, for some reason).
class JumpFixTableViewController: UITableViewController {
// There is a known issue on iOS 8 with the cell jumping when a new
// view controller is pushed. Documented here:
// http://stackoverflow.com/questions/25937827/table-view-cells-jump-when-selected-on-ios-8
// http://stackoverflow.com/questions/26370879/ios-8-uitableview-self-sizing-cells-jump-shift-visually-when-a-new-view-controll
// http://stackoverflow.com/questions/26598453/ios-8-auto-cell-height-didselectrowatindexpath-causes-uitableview-to-jump-to-t
//
// Reloading data before the view disappears is a workaround.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
// Reload data preserving selection
if let indexPath = tableView.indexPathForSelectedRow() {
tableView.reloadData()
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
}
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
// Deselect the row here instead of -tableView:didSelectRowAtIndexPath:
// to preserve the selected state until the new view controller has
// been pushed onto the navigation stack.
if let indexPath = tableView.indexPathForSelectedRow() {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
}
@ChrisBuchholz
Copy link

Is seems to fix the problem with the visible shifting as the navigationcontroller pushes a view controller onto the stack, but it still seems to shift the cells, so that when one pops the view controller, the tableview is not at the same scroll position as it were when you selected the cell.

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