collectionViewLayout

Я использую этот код для создания UICollectionViewController:

import UIKit

class ShelfViewController11: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var imageCount: [UIImage] = [UIImage(named: "b1.png")!, UIImage(named: "b2.png")!, UIImage(named: "b3.png")!,
                                 UIImage(named: "b4.png")!, UIImage(named: "b5.png")!, UIImage(named: "b6.png")!,
                                 UIImage(named: "b7.png")!, UIImage(named: "b8.png")!, UIImage(named: "b9.png")!,
                                 UIImage(named: "b10.png")!, UIImage(named: "b11.png")!, UIImage(named: "b12.png")!]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView?.showsVerticalScrollIndicator = false
    }

    // MARK: UICollectionViewDataSource

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


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ShelfViewCell
        
        cell.cellImageView.image = imageCount[indexPath.row]
        
        return cell
    }

}

// MARK: - UICollectionViewDelegateFlowLayout

extension ViewController: UICollectionViewDelegateFlowLayout {
    private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 50, height: 50)
    }
}

Но в extension func collectionViewLayout sizeForItemAtIndexPath не работает. Если я использую брейкпоинт в этой функции, мое приложение вылетает, значит функция вызывается. Но моя ячейка не меняет размер на этот return CGSize (width: 50, height: 50). Что я делаю не так?


Ответы (1 шт):

Автор решения: Oleg Soloviev

Попробуйте заменить extension на этот:

extension ShelfViewController11 {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 50, height: 50)
    }
}
→ Ссылка