SectionKit 1.1.0

SectionKit 1.1.0

Aleksey 维护。



  • 作者
  • konshin

SectionKit

Version License Platform Carthage compatible

使用方法

final class ViewController: UIViewController, SectionsAdapterDataSource {
    
    private lazy var sectionsAdapter: SectionsAdapter = {
        let adapter = SectionsAdapter(collectionView: collectionView, viewController: self)
        adapter.dataSource = self
        return adapter
    }()
    
    private lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: self.view.bounds,
                                    collectionViewLayout: UICollectionViewFlowLayout())
        view.backgroundColor = .white
        
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collecitonView.frame = view.bounds
        view.addSubview(collecitonView)
        sectionsAdapter.reloadData(animated: false)
    }

    // MARK: - SectionsAdapterDataSource
    
    func sectionGroups() -> [SectionsGroupPresentable] {
        let sections = (0..<5).map { _ in
            Section()
        }
        return [CommonSectionsGroupPresenter(sections)]
    }
}

final class Cell: UICollectionViewCell {}

final class Section: SectionPresentable {
    
    weak var sectionsContext: SectionsDisplayable?

    var insets: UIEdgeInsets {
        return UIEdgeInsets(top: 12, left: 16, bottom: 8, right: 16)
    }

    func numberOfElements() -> Int {
        return 3
    }
    
    func cellType(at index: Int) -> SectionReusableViewType<UICollectionViewCell> {
        return .code(Cell.self)
    }
    
    func configure(cell: UICollectionViewCell, at index: Int) {
        guard let cell = cell as? Cell else { return }
        
        // configure cell
    }
    
    func select(at index: Int) {
        print("Did select cell at index: \(index)")
    }
    
    func sizeForCell(at index: Int, contentWidth: CGFloat) -> SizeCalculation {
        // Width = collectionView.bounds.width - insets.left - insets.right
        // Height depends on constraints and the content
        return .automaticHeight()
    }
    
}

示例

为了运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install

安装

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
    pod 'SectionKit' ~> '1.0.5'
end

Carthage

将此代码添加到 Cartfile

github "konshin/SectionKit" "1.0.5"
$ carthage update --use-xcframeworks

作者

konshin, [email protected]

许可证

SectionKit 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。