CollectionViewCells не появляются программно - Swift 5 - PullRequest
1 голос
/ 25 апреля 2020

Я хочу показать collectionView, который появляется с анимацией после нажатия кнопки.

CollectionView отображается правильно, но collectionViewCells внутри этого не делает.

Я правильно установил методы UICollectionViewDelegate и UICollectionViewDatasource, но они не вызываются (проверено с помощью breakPoints).

Я делаю все программно , поэтому мне также пришлось зарегистрируйте SettingCell, который является общим UICollectionViewCell для соответствующего cellID.

Также я подумал, что может быть проблема, так как я слишком поздно устанавливал делегат и источник данных, но я так не думаю, так как они установлены в начале в начале класса матери.

class SettingsLauncer : NSObject, UICollectionViewDelegate, UICollectionViewDataSource{

var collectionView : UICollectionView = {
    let layout = UICollectionViewLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    return cv
}()


let CelliD = "CelliD"


func handleMoreButton(){
            //creating and adding  View
    if let windowView = UIApplication.shared.keyWindow {

        let width = UIScreen.main.bounds.width
        let height = UIScreen.main.bounds.height
        let heightCollectionView = CGFloat(300)
        let myHeight = height - heightCollectionView

        collectionView.frame = CGRect(x: 0, y: height, width: width, height: 0)

        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {

            windowView.addSubview(self.collectionView)

            self.collectionView.frame = CGRect(x: 0, y: myHeight, width: width, height: heightCollectionView)
        }, completion: nil)
    }
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CelliD, for: indexPath)

    return cell
}



override init(){
    super.init()
    collectionView.register(SettingCell.self, forCellWithReuseIdentifier: CelliD)
    collectionView.delegate = self
    collectionView.delegate = self

}
}

1 Ответ

2 голосов
/ 25 апреля 2020

, пожалуйста, добавьте эти две строки в ваш метод инициализации

    self.collectionView.delegate = self
    self.collectionView.dataSource = yourDataSource

в настоящее время у вас есть

    collectionView.delegate = self
    collectionView.delegate = self

Также сделайте свой collectionView ленивым ... и назначьте там delgates

private lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    cv.delegate = self
    cv.dataSource = yourDataSource
    cv.translatesAutoresizingMaskIntoConstraints = false
    return cv
}()
...