2011-11-14 22 views
4

我有3个UIViews堆叠在另一个上堆叠UITableViews没有通过触摸事件视图下方

的UITableView
的PlaneView
rootView

TableView中在顶部和rootView在底部的顶部上。 (rootView是不可见,因为TableView中是在它的上面)

我已经实现了下面的代码在rootView

/*code in rootView*/ 



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

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

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

期待的是,这些功能将在最顶部的视图即TableView中被触摸或被称为移动,但相反,没有任何功能被调用。

我也试图把下面的代码在TableView中,这样的rootView方法被称为

/*code in TableView so that the rootView methods are called(TableView is the subview of rootView)*/ 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[super touchesBegan:touches withEvent:event]; 
[self.superview touchesBegan:touches withEvent:event]; 
} 

正如预期它这样做,但问题是,TableView中代表像

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  

不调用。

有什么办法可以确保在TableView类(didSelectRow :)中实现的TableView委托和rootView中的touchesBegan:,touchesMoved ..函数也被相应调用?

ie当我点击一个TableCell时,两个(didSelectRow:atIndex)函数在 - > TableView和(touchesBegan和touchesEnd)方法中 - > rootView被调用。

回答

5

在你的子类UITableView你应该有触摸方法是这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
    [super touchesBegan:touches withEvent:event]; 
} 

这里的区别是,你传递触摸到下一个响应者,而不是上海华,和你这样做之前将触摸传递给超级。

然后在planeView你需要通过触摸这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.superview touchesBegan:touches withEvent:event]; 
} 

请记住,这仍然可能不会完全按照你期望的工作。 UITableView在引擎盖下做了很多响应链的修改,以使它看起来好像是一个UITableView(它实际上是一个复杂的子视图集合)只是另一个视图,如按钮或标签。

+0

伟大resolution.Thanks的帮助 – prajul

+0

但仍在某个时间点touchesEnded:不叫。我已经做了一些调整touchesEnded:消息的视图位置,但由于消息不是总是通过视图调整不按我的预期工作。 – prajul

+0

@prajul:是的,我现在正在摔跤类似的问题。表格视图是一个非常复杂的野兽。根据你使用的底层触摸,我尝试过的另一件事是将一个完全透明的UIView放在我的表格视图之上。如果你像上面的代码那样通过触摸,表格视图仍然不能正常工作(你可以点击它,但不能以任何方式滚动)。但是,如果您通过'hitTest:withEvent:'传递,则表视图将像正常一样工作,并且您可以在叠加uiview中使用初始hitTest来执行某些操作,如单击按钮或其他。 – MusiGenesis