Swift:collectionviewcell 内的操作按钮 [英] Swift: Action button inside a collectionviewcell

查看:20
本文介绍了Swift:collectionviewcell 内的操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a button inside a collectionview cell that I programmatically created.

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

    }

j

 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").我错过了什么

But when I click on the button, nothing happens (it doesn't show "Hello Edit Button"). What am I missing

推荐答案

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

There are a few things you should do differently, but this works fine for me (pretty much just the code you posted):

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
    }

}

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

If you can see your button in your collection view cells, then your code (assuming you have the cell being created correctly) should have worked.

事实上你也有这条线:

cell.bringSubview(toFront: editButton)

有点让我觉得你没有看到按钮......可能你的 Y 坐标 241 将它放在单元格框架之外?

kinda makes me think you don't see the button... Possibly your Y coordinate of 241 places it outside the cell frame?

这篇关于Swift:collectionviewcell 内的操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆