简体   繁体   English

MDCCardCollectionCell不调用didSelect方法-Swift

[英]MDCCardCollectionCell doesn't call didSelect method - swift

I am trying to custom UICollectionViewCell by using material-components called MDCCardCollectionCell . 我想定制UICollectionViewCell使用material-components称为MDCCardCollectionCell But when i implement it successfully which means when i click on each cell i can see animate and so on.. But one issue after implemented that class MDCCardCollectionCell then method didSelect is never call when i click on each and every cell. 但是,当我成功实现它时,这意味着当我单击每个单元格时,我可以看到动画等。但是在实现该类MDCCardCollectionCell之后,当我单击每个单元格时,永远不会调用方法didSelect

Before it was working fine, after i implemented MDCCardCollectionCell then the issue occurs. 在运行良好之前,我实现了MDCCardCollectionCell之后便出现了问题。 How to solve this particular issue? 如何解决这个特定问题?

Here is what i have done so far 这是我到目前为止所做的

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ExploreCellData

    cell.exploreData = exploreCategory?.data![indexPath.item]

    cell.layer.shouldRasterize = true
    cell.layer.rasterizationScale = UIScreen.main.scale
    cell.isSelectable = true
    cell.cornerRadius = 8
    cell.setShadowElevation(ShadowElevation(rawValue: 6), for: .selected)
    cell.setShadowColor(UIColor.black, for: .highlighted)
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let data = exploreCategory?.data![indexPath.item] {
        if let titleImage = data.imageName {
            print(titleImage)
            // This will push to another view controller after selected
            dashboardViewController?.showDetailEachItem(data: titleImage, imageContent: data.titleImage!, imageDesc: data.descImage!)
        }

    }

}

ExploreCellData is a custom cell using MDCCardCollectionCell ExploreCellData是使用MDCCardCollectionCell的自定义单元格

class ExploreCellData: MDCCardCollectionCell {

var exploreData: ExploreDataSection? {
    didSet{
        if let image = exploreData?.imageName {

            viewsProperty.exploreImageView.image = UIImage(named: image)
            viewsProperty.exploreNameLabel.text = exploreData?.titleImage
        }
    }
}

var viewsProperty = ExploreCategoryComponents()



let nameLabelView: UIView = {

    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 5
    view.layer.masksToBounds = true
    view.backgroundColor = .white

    return view
}()



override init(frame: CGRect) {
    super.init(frame: frame)


    setupConstraint()
}

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



}
extension ExploreCellData {

func setupConstraint() {

    addSubview(viewsProperty.cardView)

    viewsProperty.cardView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)

    viewsProperty.cardView.addSubview(viewsProperty.exploreImageView)
    viewsProperty.cardView.addSubview(nameLabelView)
    viewsProperty.cardView.addSubview(viewsProperty.exploreNameLabel)

    viewsProperty.exploreImageView.frame = CGRect(x: 5, y: 5, width: frame.width - 10, height: frame.height / 1.5)

    //        nameLabel.frame = CGRect(x: frame.width / 5, y: imageView.frame.maxY + 2, width: frame.width, height: frame.height / 4)

    viewsProperty.exploreNameLabel.topAnchor.constraint(equalTo: viewsProperty.exploreImageView.bottomAnchor).isActive = true
    viewsProperty.exploreNameLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    viewsProperty.exploreNameLabel.bottomAnchor.constraint(equalTo: viewsProperty.cardView.bottomAnchor).isActive = true
    viewsProperty.exploreNameLabel.textAlignment = .center


//        viewsProperty.exploreNameLabel.centerYAnchor.constraint(equalTo: nameLabelView.centerYAnchor).isActive = true
//        viewsProperty.exploreNameLabel.centerXAnchor.constraint(equalTo: nameLabelView.centerXAnchor).isActive = true
  }

} }

in the collection view didSelect try calling the class type 在集合视图中didSelect尝试调用类类型

   let cell = collectionView.cell(indexPath.row) as? ExploreCellData
    if let data = cell.exploreCatagory.......

also, try setting the cells isSelectable Here's a website that will help if these don't work: https://material.io/develop/ios/components/cards/api-docs/Classes/MDCCardCollectionCell.html#/c:objc(cs)MDCCardCollectionCell(py)selectable 另外,请尝试设置单元格isSelectable这是一个网站,如果这些按钮不起作用,它将提供帮助: https : //material.io/develop/ios/components/cards/api-docs/Classes/MDCCardCollectionCell.html#/c : objc (cs)MDCCardCollectionCell(py)可选

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM