4

I have a table view, and I'm adding a sub view directly to the table view with addSubview:. I am displaying this view at the position CGRectMake(0.0f, -30.0f, tableView.frame.size.width, 30.0f) (so this displays above the scrollable area of the table, when the user scrolls slightly beyond the bounds of the scroll area). I then do this:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if(scrollView.contentInset.top + scrollView.contentOffset.y < -30.0f) {
        [scrollView setContentOffset:CGPointMake(1.0f, -30.0f)-scrollView.contentInset.top animated:NO];
    }
}

So that when a user scrolls up and into the negative space in the table view, that when they release, they can still see the view I insert at y=-30 so that the table does not "snap back" to 0.

This all works exactly like I want it to. However, when the table's contentOffset is set to a negative y value such as [scrollView setContentOffset:CGPointMake(1.0f, -30.0f)-scrollView.contentInset.top animated:NO], then touches are NOT recognized on both the cells of my table, nor the view I inserted directly into the table. I am wondering how I can set contentOffset to a value such as this, and still have child views of my tableView, as well as cells, register touches.

jszumski
  • 7,430
  • 11
  • 40
  • 53
David Zorychta
  • 13,039
  • 6
  • 45
  • 81

1 Answers1

1

It might be easier to just add 30.0f to contentInset.top so that you can continue to use a stock table view.

If you still want use the approach you outlined, you need to override hitTest:withEvent: in a UITableView subclass:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.theViewInQuestion.frame, point)) {
        return self.theViewInQuestion;
    }

    return [super hitTest:point withEvent:event];
}

By default iOS views don't respond to touches outside their bounds, so what the code does is tell the event handling system that you consider theViewInQuestion to be inside of the table even though it is above the origin and out of bounds.

"Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related?" has some great answers on how the system works if you want additional detail.

Community
  • 1
  • 1
jszumski
  • 7,430
  • 11
  • 40
  • 53