不工作UIButton来自UICollectionViewCell
UIButton我在UICollectionViewCell中,但它不工作,当我点击它去另一个ViewController鄙视我访问它。我发布我的两个班级代码。为什么它发生?不工作UIButton来自UICollectionViewCell
类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)
}
}
试试这个:
解决方案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)
在cellItemAtindexPath中显示错误 –
在此处添加错误消息。 – KKRocks
:类型'DetailController'的值没有成员'giveReviewBtnTarget' –
你需要为你的创建委托,然后打开showAppDetailForApp从你的DetailController。 – GeneCode
@GeneCode感谢您的回答,请尝试更多详细信息 –
这段代码中会发生什么,您可以详细说明一下,以及您想要做什么? – KKRocks