iOS:单击TableCellView中的视图
我在UITableViewCell中放置了一个小UIView 如果点击这样的UIView,我想打开一个弹出窗口。 如果在这样的UIView之外点击,我希望执行UITableView didSelectRowAtIndexPath函数中定义的操作。 现在发生的是,当我点击视图时,两件事都发生了:弹出窗口被打开,didSelectRowAtIndexPath函数被触发 如何确保单击该UIView时未触发didSelectRowAtIndexPath函数 目前的执行情况: 我在自定义UITableViewCell类中为UIView定义了UIAPTgestureRecognitor 让我们点击手势=UIAPTgestureRecognitizerTarget:self,action:selectorMyCustomTableViewCell。单击帮助:iOS:单击TableCellView中的视图,ios,swift,uitableview,didselectrowatindexpath,Ios,Swift,Uitableview,Didselectrowatindexpath,我在UITableViewCell中放置了一个小UIView 如果点击这样的UIView,我想打开一个弹出窗口。 如果在这样的UIView之外点击,我希望执行UITableView didSelectRowAtIndexPath函数中定义的操作。 现在发生的是,当我点击视图时,两件事都发生了:弹出窗口被打开,didSelectRowAtIndexPath函数被触发 如何确保单击该UIView时未触发didSelectRowAtIndexPath函数 目前的执行情况: 我在自定义UITableVi
myClickAreaUIView.AddgestureRecognitizerTap手势尝试将内部视图的属性设置为true
只需在CellForRowatineXpath中为did select PROTECT:编写以下代码:
cell.selectionStyle = UITableViewCellSelectionStyle.None
我建议实现如下UIgestureRecognitizerDelegate方法
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
print("in shouldReceiveTouch")
gestureRecognizer.delegate = self
if ([...]){ // touch location is in the UIView
print("touching UIView")
return false // prevent from forwarding touch to superview
} else {
println("touching elsewhere")
return true
}
}
您应该从UIgestureRecognitzerDelegate使用此方法
为UIView创建一个customClass,比如UIView类型的CustomView 将UIView的@IBOutlet连接到CustomView类型的tableViewCell 在CustomView类中调用此函数
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
super.pointInside(point, withEvent: event)
return CGRectContainsPoint(self.bounds, point)
}
这将告诉您用户点击的位置是否包含在UIView边界内,如果返回true,则会在UIGestureRecognitor函数中弹出并跟随,如果为false,则执行相反的操作我建议的最佳解决方案是将UIView更改为UIButton,并使用按钮上的touchUpInside来处理触摸
通过这种方式,您将达到您的目标,因为UIButton自动阻止触摸事件转发到superview我是否可以建议将UIView更改为UIButton,并使用按钮上的touchUpInside来处理触摸?您在视图上处理UIApside的位置和方式?我认为有一种方法可以设置视图,使其不转发触摸事件。我认为按钮在默认情况下可以做到这一点,但视图之类的东西却不能做到。是的,我目前实际上正在使用UICapgestureRecognitizer添加代码,说明如何添加Tap手势和管理单元格,因为默认行为不会产生您所提到的结果。我的意思是,例如,你有一个单元格,你把uiview放在里面,在上面添加手势识别器,然后点击该视图,它将不会被称为didselectrowatindexpath。这是默认行为。DidSelectRowatineXpath也必须工作,如果用户在子视图外点击,您确定它会继续工作吗?我刚刚尝试过,DidSelectRowatineXpath仍然会触发。我期待着解决您的问题,我无法检查它,这意味着要编写一个全新的应用程序,但它工作了吗?嗨,ddb!我通过将UIView更改为UIButton并使用按钮上的touchUpInside处理触摸,解决了这个问题。如果你打算写一个关于这个问题的答案,我会接受它作为这个问题的正确答案。非常感谢!你能写另一个答案吗?你可以单独指定解决方案吗?通过接受这个答案,我也会接受第一部分,我还没有验证过。这个答案建议改变他们当前的实现。有些设计可能无法使用UIButtons而不是UIViews。@programmingandroid您可能是对的,但很少有人试图让UIView在不使用UIButton的情况下表现为UIButton:我还必须说,UIButton就是UIView
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
super.pointInside(point, withEvent: event)
return CGRectContainsPoint(self.bounds, point)
}