简体   繁体   English

Swift Collcetionview didSelectItemAtIndexPath 不工作

[英]Swift Collcetionview didSelectItemAtIndexPath not working

In myscenario, I am using UICollectionView in Storyboard.在 myscenario 中,我在 Storyboard 中使用UICollectionView The didSelectItemAtIndexPath not working. didSelectItemAtIndexPath不起作用。 I have added Imageview on UICollectionView and using customcell.我在Imageview上添加了 Imageview 并使用 customcell。

CollectionView CustomCell集合视图自定义单元格

class CustomCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.layer.cornerRadius = self.frame.size.width / 2
    }
}

My CollectionView Delegates我的 CollectionView 代表

class ModernViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

        @IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
     super.viewDidLoad()

     collectionView.delegate = self
     collectionView.dataSource = self
     collectionView.allowsSelection = true
}
        // MARK: CollectionView Delegate
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
       }

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

       func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CustomCollectionViewCell
            let item = self.listData[indexPath.item]
            let url = item.profileimage
            cell.imageView?.sd_setImage(with: URL(string: url ?? "sample.png"), placeholderImage: UIImage(named: "sample.png"))
            return cell
       }

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

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("You selected cell #\(indexPath.item)!")
            //let item = self.listData[indexPath.item]
            //print("SELECTED COLLECTION CELL: \(item.firstname ?? "")")
        }
}

remove this line from collectionViewCell class从 collectionViewCell class 中删除此行

self.layer.cornerRadius = self.frame.size.width 

updated code更新代码

class CustomCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        // self.layer.cornerRadius = self.frame.size.width / 2 // remove this line 
    }
}

I think you need to set cornerRadius of imageView instead of cell.我认为您需要设置 imageView 的cornerRadius 而不是单元格。

 self.imageView.layer.cornerRadius = self.frame.size.width/ 2

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

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