FlexibleCollectionViewController
  • October 9, 2023

Swift library of generic collection view controller with external data processing of functionality, like determine cell’s reuseIdentifier related to indexPath, configuration of requested cell for display and cell selection handler etc

Example


Initialisation with configuration

_flexibleCollectionVC = FlexibleCollectionViewController(collectionViewLayout: CustomFlowLayout(), configuration: CollectionConfiguration(userInteractionEnabled: true, showsHorizontalScrollIndicator: false, isScrollEnabled: true, multipleTouchEnabled: false, backgroundColor: .clear))

Cell and supplementary view registering

_flexibleCollectionVC.register(UICollectionViewCell.self, forCellWithReuseIdentifier: “UICollectionViewCell”)
_flexibleCollectionVC.registerSupplementaryView(UIHeaderImageCollectionView.self, kind: .header, reuseIdentifier: UIHeaderImageCollectionView.reuseIdentifier)

Requesting indentifier of cell for specific indexPath

_flexibleCollectionVC.requestCellIdentifier = { value in
return “UICollectionViewCell”
}

Requesting indentifier of supplementary view for specific indexPath

_flexibleCollectionVC.requestSupplementaryIdentifier = { value in
return UIHeaderImageCollectionView.reuseIdentifier
}

Configuration of input cell with related data to indexPath

_flexibleCollectionVC.configureCell = { (cell: UICollectionViewCell, data: CollectionImageCellData?, indexPath: IndexPath) in
guard let data = data else {
return false
}

cell.backgroundColor = data.color

return true
}

Configuration of supplementary view with related kind and data to indexPath

_flexibleCollectionVC.configureSupplementary = { (view: UICollectionReusableView, kind: SupplementaryKind, data: CollectionImageCellData?, indexPath: IndexPath) in
if let view = view as? UIHeaderImageCollectionView, let data = data {
view.text = data.category

return true
}

return false
}

Process cell selection to related indexPath

_flexibleCollectionVC.cellDidSelect = { value in
return (deselect: true, animate: true)
}

Estimate cell size

_flexibleCollectionVC.estimateCellSize = { value in
guard let layout = value.1 as? UICollectionViewFlowLayout else {
return nil
}

let col: CGFloat = 3
let width = value.0.bounds.width-(layout.sectionInset.left+layout.sectionInset.right)
let side = round(width/col)-layout.minimumInteritemSpacing
return CGSize(width: side, height: side)
}

Put predefined cells in generated order related to ListGenerator

_flexibleCollectionVC.setData(getData())

Requirements


Swift 3 or above

Installation


FlexibleCollectionViewController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod “FlexibleCollectionViewController”

GitHub


View Github

#collectionview #flexible #indexpath #swift3
YOU MIGHT ALSO LIKE...
🧭 NavigationKit

NavigationKit is a lightweight library which makes SwiftUI navigation super easy to use. 💻 Installation 📦 Swift Package Manager Using Swift Package Manager, add ...

swiftui-navigation-stack

An alternative SwiftUI NavigationView implementing classic stack-based navigation giving also some more control on animations and programmatic navigation. NavigationStack Installation ...

Stinsen

Simple, powerful and elegant implementation of the Coordinator pattern in SwiftUI. Stinsen is written using 100% SwiftUI which makes it ...

SwiftUI Router

With SwiftUI Router you can power your SwiftUI app with path-based routing. By utilizing a path-based system, navigation in your app becomes ...

FlowStacks

This package takes SwiftUI's familiar and powerful NavigationStack API and gives it superpowers, allowing you to use the same API not just ...