不能从UICollectionViewCell工作UIButton

时间:2017-07-18 05:09:37

标签: ios swift uibutton

我在UICollectionViewCell制作的UIButton,但它根本不起作用,当我点击它去另一个ViewController鄙视我访问它。我发布了我的两个类代码。为什么会这样?

Detail 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
}

}

class 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)

}

}

2 个答案:

答案 0 :(得分:0)

试试这个:

解决方案1 ​​

在ViewController中

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)

答案 1 :(得分:0)

对于仍然在努力解决这个问题的人,

Just need to extend:  UICollectionViewDelegateFlowLayout

实施例

class YourClassName: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

...

}