【问题标题】:How to make custom IGListSectionController如何制作自定义 IGListSectionController
【发布时间】:2017-04-24 13:21:38
【问题描述】:

我正在探索 IGListKit,我正在尝试使用 IGListSectionController 子类来实现这一点:

基本上,每个彩色区域都是一个 UICollectionViewCell。我已经做了一些没有绿色区域的事情。但是,我想要的是上面示例中的布局。我该怎么做?

【问题讨论】:

    标签: ios iphone iglistkit


    【解决方案1】:

    我找到了答案,其实很简单。因此,(就像在 UICollectionView 中一样)它必须使用自定义 UICollectionViewLayout 进行设置。所以我有这样的事情:

    let collectionView:IGListCollectionView = {
        let layout = MyCustomLayout()
        let view = IGListCollectionView(frame: .zero, collectionViewLayout: layout)
        return view
    }() 
    

    这是我的自定义布局的样子:

    protocol MyCustomLayoutDelegate {
        func heightForItem(inCollectionView: UICollectionView, at position: Int) -> CGFloat
    }
    
    class MyCustomLayout: UICollectionViewLayout, MyCustomLayoutDelegate {
    
        var delegate:MyCustomLayoutDelegate?
    
        private var cache = [UICollectionViewLayoutAttributes]()
    
        private var aHeight:CGFloat = 0
    
        private var contentWidth:CGFloat {
            return collectionView!.bounds.width
        }
    
        override func prepare() {
    
            if cache.isEmpty {
    
                var yOffset:CGFloat = 0
    
                for section in 0 ..< collectionView!.numberOfSections {
                    let contentHeight = (delegate ?? self).heightForItem(inCollectionView: collectionView!, at: section)
                    let leftAreaWidth = contentHeight - 1
                    let topAreaHeight:CGFloat = 20.0
                    let bottomAreaHeight = contentHeight - topAreaHeight - 1
                    let bottomAreaWidth = contentWidth - leftAreaWidth
    
                    let leftAreaIndexPath = IndexPath(item: 0, section: section)
                    let topAreaIndexPath = IndexPath(item: 1, section: section)
                    let bottomAreaIndexPath = IndexPath(item: 2, section: section)
    
                    let leftAreaFrame = CGRect(x: 0, y: yOffset, width: leftAreaWidth, height: leftAreaWidth)
                    let topAreaFrame = CGRect(x: leftAreaWidth, y: yOffset, width: bottomAreaWidth, height: topAreaHeight)
                    let bottomAreaFrame = CGRect(x: leftAreaWidth, y: yOffset + topAreaHeight, width: bottomAreaWidth, height: bottomAreaHeight)
    
                    let leftAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: leftAreaIndexPath)
                leftAreaAttributes.frame = leftAreaFrame
                cache.append(leftAreaAttributes)
    
                    let topAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: topAreaIndexPath)
                topAreaAttributes.frame = topAreaFrame
                cache.append(topAreaAttributes)
    
                    let bottomAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: bottomAreaIndexPath)
                bottomAreaAttributes.frame = bottomAreaFrame
                cache.append(bottomAreaAttributes)
    
                    yOffset += contentHeight
                }
    
                aHeight = yOffset
            }
        }
    
        override var collectionViewContentSize: CGSize {
            return CGSize(width: contentWidth, height: aHeight)
        }
    
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    
            return cache.filter {
                $0.frame.intersects(rect)
            }
        }
    
        override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    
            return cache.first {
                $0.indexPath == indexPath
            }
    
        }
    
        override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    
            if let oldWidth = collectionView?.bounds.width {
                return oldWidth != newBounds.width
            }
    
            return false
        }
    
        override func invalidateLayout() {
            super.invalidateLayout()
    
            cache = []
            aHeight = 0
        }
    
        //Delegate
        internal func heightForItem(inCollectionView: UICollectionView, at postion: Int) -> CGFloat {
            return 0
        }
    }
    

    【讨论】:

    • 感谢 yoninja 的分享。我有同样的要求,请您详细分享您对自定义布局的要求。
    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多