ios - 找出 UICollectionView 单元格中的多个按钮中的哪个被点击

标签 ios swift uicollectionview uicollectionviewcell

在我的 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/

相关文章:

ios - 为什么 UICollectionView 的委托(delegate)方法不起作用?

ios - 如何将静态元素添加到 UICollectionViewController 或使它们与其共享同一屏幕

ios - Openssl iOS 缓冲区限制

ios - 如何将 UIPickerView 选项连接到各个页面

swift - -[NSObject description] 的 Swift 等价物是什么?

ios - 从 UIViewController 切换到 SKSCene 没有显示任何内容

ios - "Do Not Embed"、"Embed & Sign"、"Embed Without Signing"。这些是什么?。他们做什么?

ios - Xcode 4 - 包含第 3 方静态库时找不到头文件

ios - TableView 单元格的 subview 的框架更改不会发生,直到 View 出现

ios - UICollectionView 组合布局 - iPad 旋转时项目大小未正确更新