Swift:集合视图中的Action按钮

时间:2017-05-03 17:56:22

标签: ios swift uicollectionviewcell

我在一个以编程方式创建的collectionview单元格中有一个按钮。

  @IBAction func editButtonTapped() -> Void {
        print("Hello Edit Button")

    }

Ĵ

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

 let editButton = UIButton(frame: CGRect(x: 8, y: 241, width: 154, height: 37))
            editButton.setImage(UIImage(named: "background-1 (dragged).tiff"), for: UIControlState.normal)
            editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside)
            editButton.tag = indexPath.row
            editButton.isUserInteractionEnabled = true

            cell.addSubview(editButton)
            cell.bringSubview(toFront: editButton)
}

但是当我点击按钮时,没有任何反应(它没有显示“Hello Edit Button”)。我缺少什么

2 个答案:

答案 0 :(得分:2)

你应该做一些不同的事情,但这对我来说很好(几乎只是你发布的代码):

class TestCollWithBtnsCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    @IBAction func editButtonTapped() -> Void {
        print("Hello Edit Button")
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: 200.0, height: 200.0)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

        cell.backgroundColor = .orange

        // this is the WRONG way to do this...
        //  1) the button is being added avery time a cell is reused
        //  2) the button should be added to the cell.contentView (not the cell itself)
        //  3) much better ways to track than setting the button .tag to the row
        //  4) target for the button action is "self" - locks you into a bad structure
        // however, this can help us debug the problem

        let editButton = UIButton(frame: CGRect(x: 8, y: 41, width: 154, height: 37))

        // I don't have an image, so I set a button title and background color
        editButton.setTitle("\(indexPath.row)", for: .normal)
        editButton.backgroundColor = .red
//      editButton.setImage(UIImage(named: "background-1 (dragged).tiff"), for: UIControlState.normal)

        editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside)
        editButton.tag = indexPath.row
        editButton.isUserInteractionEnabled = true

        cell.addSubview(editButton)

        return cell
    }

}

如果您可以在集合视图单元格中看到您的按钮,那么您的代码(假设您正确创建了单元格)应该有效。

你也有这条线的事实:

cell.bringSubview(toFront: editButton)

有点让我认为你看到按钮... Y 241坐标可能会将它放在细胞框架之外?

答案 1 :(得分:-4)

正在发生的事情是集合视图捕获触摸事件并处理它们。您需要指定集合视图需要将触摸事件传递给其子类。

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    //loop through the cells, get each cell, and run this test on each cell
    //you need to define a proper loop
    for cell in cells {

         //if the touch was in the cell
         if cell.frame.contains(point) {
            //get the cells button
            for uiview in cell.subviews {
                if uiview is UIButton {
                   let button = uiview as! UIButton
                   //if the button received the touch, return it, if not, return the cell
                    if button.frame.contains(point) {
                       return button
                    }
                    else {
                       return cell
                    }
                 }
             }
         }
    }
    //after the loop, if it could not find the touch in one of the cells return the view you are on
    return view

}