Swift Collcetionview didSelectItemAtIndexPath не работает - PullRequest
0 голосов
/ 07 ноября 2019

В myscenario я использую UICollectionView в раскадровке. didSelectItemAtIndexPath не работает. Я добавил Imageview на UICollectionView и используя 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

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

1 Ответ

0 голосов
/ 07 ноября 2019

удалить эту строку из класса 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 
    }
}

Я думаю, вам нужно установить cornerRadius для imageView вместо ячейки.

 self.imageView.layer.cornerRadius = self.frame.size.width/ 2
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...