ios - 如何制作自定义 IGListSectionController

标签 ios iphone iglistkit

我正在探索 IGListKit,并尝试使用 IGListSectionController 子类来实现此目的:

enter image description here

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

最佳答案

我找到了答案,其实很简单。因此,(就像在 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
    }
}

关于ios - 如何制作自定义 IGListSectionController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43589273/

相关文章:

iphone - MKMapView 注释更改/丢失顺序?

iphone - UrbanAirShip 中的角标(Badge)编号未更新

iphone - 在 UITextView 中禁用返回键

ios - 呈现的 UIViewController 拒绝横向布局

ios - 从视频中获取视频缩略图需要时间和从视频制作新帧时表格 View 滚动卡住

ios - 无效的索引路径错误

ios - 如何在 swift 中使用 IGListKit 创建一个两列的 Collection View 列表

ios - 运行 Cordova 运行命令时 Xcode 构建错误

iphone - 如何使用 MailCore 定期获取新邮件

ios - 无法滚动到底部 UICollectionview