1

I want to prevent my table view from scrolling to the selected cell.

For some reason when I select a cell and the keyboard appears, my cell moves up a bit and I do not want that behavior.

I am using a table view controller and set isScrollEnabled to false.

I have overridden didSelectRowAt to do absolutely nothing.

In the storyboard, I have scrolling disabled, paging disabled, bounce on scroll/zoom disabled, bounce horizontally/vertically disabled.

Yet it still scrolls to the selected cell.

ZGski
  • 2,398
  • 1
  • 21
  • 34
mocode9
  • 229
  • 3
  • 16
  • duplicate of an existing question, see this https://stackoverflow.com/questions/22447614/how-do-i-stop-a-tableview-from-scrolling-when-the-keyboard-appears and also this https://stackoverflow.com/questions/9637089/disabling-automatic-scrolling-of-uitableview-when-editing-uitextfield-inside-uit/12010951#12010951 – Wendra Apr 10 '18 at 03:25

4 Answers4

0

I would suggest you to use a UIViewController with a tableview inside. The behaviour your are describing is actually something automatically handled by the UITableViewController.

EDIT:

Here is a screenshot of a UIViewController with a tableview with static cells in it:

enter image description here

RomOne
  • 2,065
  • 17
  • 29
  • I'm using static table view cells so I can't use an embedded view controller. It had to be a table view controller. – mocode9 Apr 10 '18 at 21:10
  • oh you mean you are using a xib? because you can set static cells to a tableview embedded in a UIViewController – RomOne Apr 11 '18 at 03:54
  • Latest Xcode and Swift version wouldn't allow me to do what you describe. – mocode9 Apr 11 '18 at 23:19
  • Well I use everyday the latest Xcode and latest swift. I can tell you that it's totally possible to create a `UIViewController` in a storyboard with static cells in it. Check my edited answer. – RomOne Apr 11 '18 at 23:42
  • When I have time I'll look into what the issue was. Definitely wasn't working for me. – mocode9 Apr 12 '18 at 21:58
  • If it was not working, I'm pretty sure your were working with a .xib instead of a storyboard. In a xib, you can't add any cells in a tableview. – RomOne Apr 12 '18 at 22:59
0

ok, in my whole career as iOS developer, this might be the first case where subclassing UITableView IS the best answer!


UITableView uses a pan gesture recognizer to detect the scrolling gesture. The tableview itself must be it's delegate, therefore we can stop the gesture from being detected by subclassing UITableView:

import UIKit

class DontScrollTableView: UITableView, UIGestureRecognizerDelegate {
   
    var dontScroll = false
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return !dontScroll
    }
}

in our UITableViewDelegate implementation we can toggle scrolling:

var previouslySelectedIndexPath: IndexPath?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let tv = tableView as? DontScrollTableView {
        if let lastSelected = previouslySelectedIndexPath {
            tv.dontScroll = lastSelected != indexPath
            if lastSelected == indexPath {
                tableView.deselectRow(at: indexPath, animated: false)
                previouslySelectedIndexPath = nil
            } else {
                previouslySelectedIndexPath = indexPath
            }
        } else {
            tv.dontScroll = true
            previouslySelectedIndexPath = indexPath
        }
    }
    
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let tv = tableView as? DontScrollTableView {
        tv.dontScroll = false
    }
}
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
-1

you can avoid scrolling tableview simply by using this line of code....

tableView.isScrollEnabled = false

this line will disable scroll feature for tableview whenever you want it to scroll again set this property to true.

junaid
  • 193
  • 1
  • 16
-2

Solution was to override viewwillappear and not call the super method.

mocode9
  • 229
  • 3
  • 16
  • 1
    I Won't recommend doing that. You're violating the rules of the framework and basic Object Oriented design, and you should expect problems. – RomOne Apr 11 '18 at 23:49
  • A good example of what could happen: https://stackoverflow.com/questions/18536858/viewdidappear-presence-in-code-messes-with-layout – RomOne Apr 11 '18 at 23:57