UITableView的backgroundView上的触摸事件

3
我有一个UITableViewController,tableView使用backgroundView来展示一个视图(更具体地说是MKMapView),设置方法为:
self.tableView.backgroundView = _mapView;
目前,该背景视图已显示在屏幕上(我也有一个透明的表头来实现这一点)。 我无法使mapView(tableView的backgroundView)对用户交互做出响应。我的表头位于地图视图之上,并且是UIView的子类,具有以下覆盖方法:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    LogDebug(@"Clic en transparent view");
    if (_transparent)
    {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

所以它应该通过这些事件;我不知道错在哪里,并且在其他类似的问题中也没有找到任何解决办法。

必须将mapView保留为tableView的背景属性,请考虑这一点。

有什么想法吗?


你有没有得到任何解决方案,我的朋友? - santhu
5个回答

2

回答这个问题的时间有点晚了。但是我在自己的项目中遇到了这个问题,经过一些研究后找到了答案。首先,我没有将背部视图指定为 tableView.backgroundView,而是让 tableView.backgroundColor 变成透明的,然后在表视图下方放置另一个视图。

编辑: 当我说“在表格下面”时,我的意思是 tableViewanotherView 是相同父视图的子视图,并且 tableView.zPosition 大于 anotherView.zPosition

然后我只需要重写 tableView

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

并让 tableView 避免持有不在其单元格中的事件。

以下是代码:

-(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);

}

对我来说这个东西非常好用。希望它能帮助其他遇到这个问题的人。


那么在表视图下面再放置另一个视图。你能展示一下代码吗?我不明白你的意思。 - malhal

0

我猜觉得触摸事件无法传递到tableView的backgroundView。

所以,我做的是,在HeaderView中添加了一个名为mapView的类属性,它是UIVIEW的子类。

在HeaderView.h文件中:

@property (nonatomic,assign) UIView *mapView;

而在tableViewController

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

在HeaderView中覆盖hitTest方法并返回此地图视图。
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    return self.mapView;
}

现在mapView接收到该touchEvent并做出相应的响应。
添加您感兴趣的其他代码。

希望这可以帮助到您。

0

Santhu的答案几乎对我有用,我将hit test传递给了mapView而不仅仅是返回它,此外我还需要进行一个点检查,因为我只想让头部触摸事件通过,所以这是我的HeaderView上的hit test版本:

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

0
将此代码放入UITableView子类中,缺点是您不能再点击单元格,但希望这可以指引您正确的方向。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

0

顺便提一下,这是一篇旧文章,UITableView.backgroundView现在确实可以接收触摸事件。因此,如果您的backgroundView中的内容没有接收到触摸事件,那么可能是由于其他问题导致的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接