首页 > 解决方案 > 当我点击单元格时,collectionView 内的视图没有隐藏

问题描述

我想根据特定单元格上的点击隐藏与 collectionView 单元格相连的底部视图。我曾尝试在“didSelectItemAt”函数上执行此操作,但它不起作用。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell

        cell.bottomView.isHidden = true    
    } 

   internal func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.lableArray.count
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
        let item = lableArray[indexPath.row]
        cell.imageView.backgroundColor = UIColor.randomColor()
        cell.lable.text = item
        return cell
    } 

在此处输入图像描述

标签: swiftcollectionview

解决方案


通过更新
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:reuseIdentifier, for: indexPath) 解决了它!集合视图单元

在 didSelectItemAt 中

让 cell = collectionView.cellForItem(at: indexPath) as!集合视图单元

并添加带有类的 UICollectionViewDelegate。


推荐阅读