CombineCocoa simplifies the connection between UIKit and Combine, allowing seamless binding of UI components to reactive data streams.
Bind UI elements likeUICollectionView
andUITableView
toPublished
data streams. Support for custom cell types using Combine. Example 1: BindingUICollectionView
to aPublished
property @Published var items = [ String] ( ) let collectionView = UICollectionView ( ) private var subscriptions = Set < AnyCancellable > ( ) collectionView. bind ( $items, cellType: SomeCell . self) { index, element, cellin cell. render ( model: element) } . store ( in: & subscriptions) Example 2: Complex cell binding with multiple cell types enum CellType { case regular( DataModel ) case header( String ) } @Published var cells = [ CellType] ( ) let collectionView = UICollectionView ( ) private var subscriptions = Set < AnyCancellable > ( ) collectionView. bind ( $cells) { collectionView, indexPath, itemsin switch items{ case . regular( let data) : return collectionView. dequeueCell ( RegularCell . self, indexPath) { cellin cell. configure ( with: data) } case . header( let title) : return collectionView. dequeueCell ( HeaderCell . self, indexPath) { cellin cell. setTitle ( title) } } } . store ( in: & subscriptions) Example 3: BindingUITableView
@Published var tableItems = [ String] ( ) let tableView = UITableView ( ) private var subscriptions = Set < AnyCancellable > ( ) tableView. bind ( $tableItems, cellType: UITableViewCell . self) { index, item, cellin cell. textLabel? . text= item} . store ( in: & subscriptions) Example 4: Using BaseCell with Subclasses Base Cell Implementation: open class BaseCell < ViewModel> : UITableViewCell { public override init ( style: UITableViewCell . CellStyle , reuseIdentifier: String ? ) { super. init ( style: style, reuseIdentifier: reuseIdentifier) commonInit ( ) } public required init ? ( coder: NSCoder ) { super. init ( coder: coder) commonInit ( ) } open func commonInit( ) { selectionStyle= . none} open func render( viewModel: ViewModel ) { } } final class UserCell : BaseCell < UserViewModel > { private let nameLabel = UILabel ( ) override func commonInit( ) { super. commonInit ( ) contentView. addSubview ( nameLabel) } override func render( viewModel: UserViewModel ) { nameLabel. text= viewModel. name} } final class ProductCell : BaseCell < ProductViewModel > { private let productLabel = UILabel ( ) override func commonInit( ) { super. commonInit ( ) contentView. addSubview ( productLabel) } override func render( viewModel: ProductViewModel ) { productLabel. text= viewModel. title} } Using Subclasses in a Table: @Published var users : [ UserViewModel ] = [ ] let tableView = UITableView ( ) private var subscriptions = Set < AnyCancellable > ( ) tableView. bind ( $users, cellType: UserCell . self) { index, user, cellin cell. render ( viewModel: user) } . store ( in: & subscriptions) Add this to yourPackage.swift
file:
dependencies: [ . package ( url: " https://github.com/KopievDev/CombineCocoa " , from: " 1.0.0 " ) ]