How do I get compact view?

How do I get a UITableView header view to come before the table cells in the responder chain?

  • I have a rather large table header view above my table view. I have a number of subviews in that header view. I am doing something a bit nonstandard where I am "sticking" some of those subviews (but not all) at the top of the table view. My problem is that although visually the table view cells pass under the sticky table header subviews, it seems that the table view cells are "above" the table header view in terms of touch response. (For example: I have a button that is a subview of the table header view. When there are no cells underneath the button, the button works great and responds to touches. However, when the user scrolls the table view so that there are cells underneath the button, a touch on the button actually selects a hidden cell rather than push the button.) Can anyone give advice on how to "raise" the table header view above the table view cells, so that its subviews get first shot at touch handling? I am using a table header view rather than a section header view due to the fact that I only want some of the subviews to stick (letting the others scroll up off the screen as usual). One of the subviews also can be expanded (and that expansion is animated) - to a height that is even bigger than the entire height of the screen. For these reasons, I didn't think using a section header view would be feasible. (If it is, please let me know, as I know that section header views are "above" table cells when it comes to touch response.)

  • Answer:

    The UIView documentation (for hitTest:withEvent:) says, Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds. The table header view appears above the first cell and, when you scroll down, moves up and off the top of the screen. If you have subviews of the table header view that you are manipulating to stick to the top of the table view, chances are they are well outside the table header view's bounds. (You probably didn't think you were going to run into a problem since clipsToBounds is by default NO.) In other words, this isn't a problem of responder chain order; this is a problem of view geometry. Your views aren't receiving touch events at all because the parent view has scrolled off the screen. Rewriting touch handling, even with gesture recognizers, is very bad idea. (Unless you have time to handle all the edge cases and also the Accessibility API.) You should split your content: the part that scrolls away should be the table header view, and the part that should stick to the top of the table should be a custom section header view. If something needs to expand and cover the entire table, it should be implemented as a new view (you can animate the transition so it looks like the same view is growing larger).

Benjamin Ragheb at Quora Visit the source

Was this solution helpful to you?

Other answers

I would try disabling selection on the table view, and reimplementing selection using a UIGestureRecognizer. Furthermore I'd use the UIGestureRecognizerDelegate protocol to conditionally block the recognizer if necessary.

Chris Hinkle

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.