【问题标题】:not work UIButton from UICollectionViewCellUICollectionViewCell 中的 UIButton 不起作用
【发布时间】:2025-12-23 15:15:09
【问题描述】:

我在 UICollectionViewCell 中制作的 UIButton,但它根本不起作用,当我单击它转到另一个 ViewController 时,我鄙视我访问它。我发布了我的两节课代码。为什么会这样?

类 DetailController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var arrDetailProduct = [Product]()


let descriptionCellid = "descriptioncellid"
let reviewAverageRateCellid = "reviewaveragerateid"
let baceCellId = "baceCellid"



override func viewDidLoad() {
    super.viewDidLoad()


    collectionView?.register(ReviewAverageRate.self, forCellWithReuseIdentifier: reviewAverageRateCellid)
    collectionView?.register(BaseCellNew.self, forCellWithReuseIdentifier: baceCellId)
}

func showAppDetailForApp(){

    let appDetailController = GiveReviewViewController()

    navigationController?.pushViewController(appDetailController, animated: true)
}


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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reviewAverageRateCellid, for: indexPath) as! ReviewAverageRate
        cell.detailControllr = self
        return cell


}


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

}

类 ReviewAverageRate: UICollectionViewCell {

var detailControllr = DetailController()
override init(frame: CGRect) {
    super.init(frame: frame)

    setupDigitalReviewAverageRate()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


let giveReviewBtn: UIButton = {

    let btn = UIButton()
    btn.setTitle("GIVE REVIEW", for: .normal)

    btn.setTitleColor(.white, for: .normal)

    btn.backgroundColor = UIColor.orange
    btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 10)
    btn.addTarget(self, action: #selector(giveReviewBtnTarget), for: .touchUpInside)

    return btn
}()

func giveReviewBtnTarget() {

    detailControllr.showAppDetailForApp()

}

func setupDigitalReviewAverageRate() {

    addSubview(giveReviewBtn)

    addConstraintsWithFormat("H:|[v0(80)]|", views:  giveReviewBtn)

    addConstraintsWithFormat("V:|[v0(20)]|", views: giveReviewBtn)

}

}

【问题讨论】:

  • 您需要为您的单元格创建委托,然后从您的 DetailController 打开 showAppDetailForApp。
  • @GeneCode 感谢您的回答,请尽量详细说明
  • 这段代码中发生了什么你能详细说明一下你想做什么吗?
  • @KKRocks 我在 UICollectionViewCell 中制作了 UIButton,在那里 addTarget 不起作用.... btn.addTarget(self, action: #selector(shareBtnTarget), for: .touchUpInside)

标签: ios swift uibutton


【解决方案1】:

试试这个:

解决方案 1

在视图控制器中

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reviewAverageRateCellid, for: indexPath) as! ReviewAverageRate
        cell.detailControllr = self
            cell.giveReviewBtn.addTarget(self, action: #selector(self.giveReviewBtnTarget), for: .touchUpInside)
        return cell
}


func giveReviewBtnTarget() {
   // add your code 

}

解决方案 2

在单元格中

            btn.addTarget(self, action: #selector(self.giveReviewBtnTarget), for: .touchUpInside)

【讨论】:

  • :“DetailController”类型的值没有成员“giveReviewBtnTarget”
  • 你需要在那里展示你的控制器。我忘记删除了。
  • 您需要同时使用这两个代码,这就是您的解决方案。
  • 你能帮我回答这个问题吗?我想通过单击按钮来检测 tableview 单元格中的一个按钮。这是链接*.com/questions/48970198/…
【解决方案2】:

对于任何仍在为这个问题苦苦挣扎的人,

Just need to extend:  UICollectionViewDelegateFlowLayout

例子

class YourClassName: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

...

}

【讨论】: