sonxurxo
sonxurxo

Reputation: 5718

Touch events on UITableView's backgroundView

I've got a UITableViewController, and the tableView has a view as backgroundView (even more, it's a MKMapView), set with

self.tableView.backgroundView = _mapView;

Currently the background view is showing on the screen (I also have a transparent table header to achieve this). I'm not able to make the mapView (tableView's backgroundView) respond to user interaction. My table header, which is above the map view, is a subclass of UIView with the following override:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    LogDebug(@"Clic en transparent view");
    if (_transparent)
    {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

so it's supposed to pass through the events; I don't know what's wrong, and didn't find any solution in other similar questions.

I must keep the mapView as the tableView's background property, so please take this into account.

Any idea?

Upvotes: 3

Views: 2151

Answers (5)

Elijah
Elijah

Reputation: 8610

FYI, this is an old post, and UITableView.backgroundView does receive touches now. So, if your content in your backgroundView isn't receiving touches it's due to some other issue.

Upvotes: 0

alvisjiang
alvisjiang

Reputation: 51

it's relatively late to answer the question. But I ran into this problem on my own project, and after some research I got the answer. First off, I didn't assign the view in the back to tableView.backgroundView. Instead, I made the tableView.backgroundColor transparent then put another view under the table view.

EDIT: by "under the table" I mean the tableView and the anotherView are children to the same parent view and tableView.zPosition is greater than anotherView.zPosition.

Then all I did is override the tableView:

(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

and let the tableView avoid holding the events that are not in its cells.

Here's the code:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

    /// the height of the table itself.
    float height = self.bounds.size.height + self.contentOffset.y;
    /// the bounds of the table. 
    /// it's strange that the origin of the table view is actually the top-left of the table.
    CGRect tableBounds = CGRectMake(0, 0, self.bounds.size.width, height);
    return CGRectContainsPoint(tableBounds, point);

}

it works like a charm for me. hope it'll help any other people who run into this problem.

Upvotes: 2

malhal
malhal

Reputation: 30773

Put this in a UITableView subclass, the downside is you can no longer tap cells but it hopefully is a pointer in the right direction.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

Upvotes: 0

Eli Kohen
Eli Kohen

Reputation: 521

Santhu answer almost worked for me, I passed hit test to mapView instead of just returning it, also I needed to made a point check because I wanted only header touch events passing through so there is my version of hit test on the HeaderView:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    if(point.y > self.frame.size.height){
        return nil;
    }
    return [self.mapView hitTest:point withEvent:event];
}

Upvotes: 0

santhu
santhu

Reputation: 4792

I guess that the touchEvents cant be passed to tableView's backgroundView. So, what i did is ,

For headerView which is a subclass of UIVIEW, i added a class property as mapView. In HeaderView.h

@property (nonatomic,assign) UIView *mapView;

And, in tableViewController

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView *view =[[TestView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    view.mapView =tableView.backgroundView;
    return view;
}

And override hitTest in HeaderView and return this mapView.

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    return self.mapView;
}

Now the mapView receives that touchEvent and responds accordingly.
.Add extra code that you might interest. Hope this helps.

Upvotes: 0

Related Questions