在我的 Swift 代码中,我有一个带有 3 个按钮的 UICollectionViewCell
(所有三个按钮都有 IBAction)。在我的 UICollectionViewController
中,我现在想“捕捉”各个按钮的点击。
我关注了这个 StackOverflow question通过将此行添加到 viewDidLoad,我可以在我的 CollectionViewController 中捕获 UICollectionViewCell 的内部修饰
gestureRecognizer.cancelsTouchesInView = false
还有这个功能
func handleTapForCell(recognizer: UITapGestureRecognizer){
//I can break in here
}
但现在缺少的部分是我如何确定三个按钮中的哪一个被点击了?我在按钮上设置了不同的标签,但我没有在处理这些标签的 gestureRecognizer 上找到任何地方。
有什么想法吗?
最佳答案
我认为,您不需要在单元格上添加手势来获得 tableviewCell 的按钮操作。此代码可以帮助您:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Your tableviewCell code here
//set tag of cell button
cell.button1.tag = 1
cell.button2.tag = 2
cell.button3.tag = 3
//add action of your cell button
cell.button1.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
cell.button2.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
cell.button3.addTarget(self, action: Selector("cellButtonTapped:event:"), forControlEvents: .TouchUpInside)
// return cell
}
func cellButtonTapped(sender:UIButton, event:AnyObject){
let touches: NSSet = event.allTouches()!
let touch = touches.anyObject()
let currentTouchPosition: CGPoint = (touch?.locationInView(YOUR_TABLEVIEW_INSTANCE))!
if let indexPath: NSIndexPath = self.YOUR_TABLEVIEW_INSTANCE.indexPathForRowAtPoint(currentTouchPosition)!{
if sender.tag == 1{
//cell first button tap
}else sender.tag == 2{
//cell second button tap
}
else sender.tag == 3{
//cell 3rd button tap
}
}
}
关于ios - 找出 UICollectionView 单元格中的多个按钮中的哪个被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266391/