Как определить, сколько ячеек должно быть отображено в UICollectionViewFlowLayout? - PullRequest
0 голосов
/ 01 октября 2019

У меня есть пользовательский подкласс UICollectionViewFlowLayout, который отображает все ячейки друг над другом (таким образом, пользователю видна только самая верхняя ячейка). Я полагаю, поскольку все представления в рамке, UICollectionView отображает все ячейки, даже если для пользователя фактически видна только одна.

Есть ли способ повлиять на количество ячеек, отображаемых UICollectionView? Я бы предпочел отображать только первые две или три ячейки.

class UICollectionViewStackLayout: UICollectionViewFlowLayout {
    override init() {
        super.init()
        self.scrollDirection = .vertical
        self.minimumInteritemSpacing = 0
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard
            let attributes = super.layoutAttributesForElements(in: rect),
            let collectionView = self.collectionView
        else {
            return nil
        }

        for attribute in attributes {
            attribute.zIndex = Int.max - attribute.indexPath.item // First item on top
            attribute.center = collectionView.center
        }

        return attributes
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return UICollectionViewLayoutAttributes(forCellWith: indexPath)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...