首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MDCCardCollectionCell不调用didSelect方法-- swift

MDCCardCollectionCell不调用didSelect方法-- swift
EN

Stack Overflow用户
提问于 2018-09-03 01:39:54
回答 1查看 455关注 0票数 1

我正在尝试使用名为MDCCardCollectionCellmaterial-components来自定义UICollectionViewCell。但是当我成功地实现它时,这意味着当我单击每个单元格时,我可以看到动画等等。但是在实现了类MDCCardCollectionCell然后方法didSelect之后的一个问题是,当我单击每个单元格时都不会调用它。

在它正常工作之前,在我实现MDCCardCollectionCell之后,问题就出现了。如何解决这个特殊的问题?

以下是我到目前为止所做的工作

代码语言:javascript
复制
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是使用MDCCardCollectionCell的自定义单元格

代码语言:javascript
复制
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
  }

}

EN

回答 1

Stack Overflow用户

发布于 2018-09-03 02:16:03

在集合视图didSelect中,尝试调用类类型

代码语言:javascript
复制
   let cell = collectionView.cell(indexPath.row) as? ExploreCellData
    if let data = cell.exploreCatagory.......

另外,尝试设置单元格isSelectable如果这些都不起作用,这里有一个网站可以提供帮助:https://material.io/develop/ios/components/cards/api-docs/Classes/MDCCardCollectionCell.html#/c:objc(cs)MDCCardCollectionCell(py)selectable

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52139520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档