gpt4 book ai didi

ios - Swift Collcetionview didSelectItemAtIndexPath 不起作用

转载 作者:行者123 更新时间:2023-11-29 05:20:09 27 4
gpt4 key购买 nike

在我的场景中,我在 Storyboard 中使用 UICollectionViewdidSelectItemAtIndexPath 不起作用。我在 UICollectionView 上添加了 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
}
}

我的 CollectionView 委托(delegate)

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 ?? "")")
}
}

最佳答案

从collectionViewCell类中删除这一行

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

更新代码

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
}
}

我认为你需要设置imageView的cornerRadius而不是cell。

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

关于ios - Swift Collcetionview didSelectItemAtIndexPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58742594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com