Swift 3 UICollectionViewCell 中的按钮不响应触摸

Swift 3 Button in UICollectionViewCell not respond to touch

我有一个 UICollectionViewCell 和里面的两个按钮,但按钮不响应触摸,我以编程方式创建按钮并通过 addSubView 将其添加到单元格它有效,但是当我添加时holder.addSubView 不工作!

holder的

UserInteractionEnabeld是真的!

请帮忙

class FavoriteProductCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var holder: UIView!


    override func awakeFromNib() {
        super.awakeFromNib()

        let btnFindBestPrice = UIButton(frame: CGRect(x: 0, y: 0, width: 151, height: 20))



        btnFindBestPrice.setTitle("Find best price", for: .normal)
        btnFindBestPrice.backgroundColor = UIColor.ButtonBackGreen

        btnFindBestPrice.isUserInteractionEnabled = true

        btnFindBestPrice.addTarget(self, action: #selector(findBestPrice), for: .touchUpInside)

        // AddTarget not working
        holder.addSubview(btnFindBestPrice)


        // AddTarget works fine
        addSubview(btnFindBestPrice)
    }
}

我不知道为什么!?但是我创建了另一个具有相同配置的单元格,但效果很好!!!!

在这种情况下,最好在开头添加self。像这样:

self.holder.addSubview(btnFindBestPrice)