Preventing “wrapping” of items in UICollectionView

前端 未结 3 1082
执笔经年
执笔经年 2021-02-09 06:09

I need a UICollectionView to display a grid that is potentially larger than the visible frame in both width and height, while maintaining row and column integrity.

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 06:31

    Here is complete matrix customLayout:

    import UIKit
    
    class MatrixLayout: UICollectionViewLayout {
    
        var itemSize: CGSize!
        var interItemSpacingY: CGFloat!
        var interItemSpacingX: CGFloat!
        var layoutInfo: Dictionary!
    
        required init?(coder aDecoder: NSCoder) {
    
            super.init(coder: aDecoder)
    
            itemSize = CGSizeMake(50.0, 50.0)
            interItemSpacingY = 1.0
            interItemSpacingX = 1.0
        }
    
        override func prepareLayout() {
    
            var cellLayoutInfo = Dictionary()
    
            let sectionCount = self.collectionView?.numberOfSections()
            var indexPath = NSIndexPath(forItem: 0, inSection: 0)
    
            for (var section = 0; section < sectionCount; section += 1)
            {
                let itemCount = self.collectionView?.numberOfItemsInSection(section)
    
                for (var item = 0; item < itemCount; item += 1)
                {
                    indexPath = NSIndexPath(forItem:item, inSection: section)
                    let itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
                    itemAttributes.frame = frameForCellAtIndexPath(indexPath)
                    cellLayoutInfo[indexPath] = itemAttributes
                }
    
                self.layoutInfo = cellLayoutInfo
            }
        }
    
        func frameForCellAtIndexPath(indexPath: NSIndexPath) -> CGRect
        {
            let row = indexPath.section
            let column = indexPath.item
    
            let originX = (self.itemSize.width + self.interItemSpacingX) * CGFloat(column)
            let originY = (self.itemSize.height + self.interItemSpacingY) * CGFloat(row)
    
            return CGRectMake(originX, originY, self.itemSize.width, self.itemSize.height)
        }
    
        override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]?
        {
            var allAttributes = Array()
    
            for (index, attributes) in self.layoutInfo
            {
                if (CGRectIntersectsRect(rect, attributes.frame))
                {
                    allAttributes.append(attributes)
                }
            }
            return allAttributes
        }
    
        override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
            return self.layoutInfo[indexPath]
        }
    
    
        override func collectionViewContentSize() -> CGSize {
    
            let width:CGFloat = (self.itemSize.width + self.interItemSpacingX) * CGFloat((self.collectionView?.numberOfItemsInSection(0))!)
            let height:CGFloat = (self.itemSize.height + self.interItemSpacingY) * CGFloat((self.collectionView?.numberOfSections())!)
    
            return CGSizeMake(width, height)
        }
    }
    

    sections are rows, items are columns

提交回复
热议问题