1

The problem: HIGHLIGHT vs SCROLLING

My buttons inside the cell where not getting highlighted when I lightly tap on them. I had to tap hard and for a long time to be able to see the tap state of the button.

So I did this in order to set the delaysContentTouches to false (I didn't manage other way to do it) inside viewDidLoad():

   for index in tableView.subviews {
        if (index.isKindOfClass(UIScrollView)) {
            let scrollViewFound = index as! UIScrollView
            scrollViewFound.delegate = self
            scrollViewFound.delaysContentTouches = false
            scrollViewFound.canCancelContentTouches = true
            scrollViewFound.scrollEnabled = true
        }
    }

This way the buttons highlight correctly but then I cannot scroll the table up or down, unless I start dragging from one of the empty cells --> userInteractionEnable = false in the empty cells


What I need:

To be able to highlight the buttons but also to scroll the table. Is it even possible to have both, scrollable view and highlighted buttons?


What I have tried

I tried calling this function:

func touchesShouldCancelInContentView(view: UIView) -> Bool {
    print("touchesShouldCancelInContentView happening---------")
    return true
}

Which never gets called. I tried overriding But it gives an error:

Method does not override any method from its superclass

Which is weird, because UITableViewController inherits from UIScrollView. I also tried adding UIScrollViewDelegate to the class definition, but of course it gives another error that this is redundant.


Extra Information

The class is declared like this:

class Settings: UITableViewController, UITextFieldDelegate { ...

The tableView is made of Static Cells

The cells:

  • Some are empty: where UserInteractionEnable = false
  • Some have buttons with text field: I want these buttons to get highlighted. UserInteractionEnable = true. The button action is called by .TouchUpInside
  • Some have labels and a check image: Their action gets called in didSelectRowAtIndexPath which will change the labels colour and check images

Maybe it is relevant to say that when user clicks on any cell didSelectRowAtIndexPath it will call a function to dismiss the keyboard.

PMT
  • 1,082
  • 8
  • 18
  • How are you setting the `UIControlAction` for the button? It should be `.TouchUpInside`. If you have it at `.TouchesBegan` or something else you won't be able to scroll. With `.TouchUpInside` your button will only be selected if the user taps within button bounds and taps back up while still in its bounds. But Scrolling a tableView would be a drag Gesture and therfore shouldn't select the button. – NSGangster Feb 04 '16 at 16:58
  • Hello @NSGangster It is called by `.TouchUpInside` I just edited the question, maybe it is relevant to say that I have a dismissKeyboard function that gets called whenever the user clicks at any cell. – PMT Feb 04 '16 at 17:43
  • Im @ work atm. I'll take a closer look at this on my break. – NSGangster Feb 04 '16 at 18:35
  • Thank you @NSGangster this is really bringing me crazy. I also think I am not subclassing the UIScrollView function right – PMT Feb 05 '16 at 09:46

1 Answers1

1

You tried to subclass the wrong class, that's why it doesn't work. You have to subclass the UITableView class itself, and not the UITableViewController. Can you try the following ?

- First

Subclass the TableView class in order to override the touchesShouldCancelInContentView function.

class UIDraggableTableView: UITableView {

    override func touchesShouldCancelInContentView(view: UIView) -> Bool {
        if (view.isKindOfClass(UIButton)) {
            return true
        }
        return super.touchesShouldCancelInContentView(view)
    }
}

- Second

In your TableViewController class, when viewDidLoad() is called, append the following right after super.viewDidLoad():

self.tableView = DraggableTableView()

This should solve your issue.

Part of this answer was taken from this StackOverflow post.

Community
  • 1
  • 1
Skwiggs
  • 1,348
  • 2
  • 17
  • 42