Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Combine+UIKit helpers

NotificationsYou must be signed in to change notification settings

KopievDev/CombineCocoa

Repository files navigation

CombineCocoa simplifies the connection between UIKit and Combine, allowing seamless binding of UI components to reactive data streams.

Features

  • Bind UI elements likeUICollectionView andUITableView toPublished data streams.
  • Support for custom cell types using Combine.

Example 1: BindingUICollectionView to aPublished property

@Publishedvaritems=[String]()letcollectionView=UICollectionView()privatevarsubscriptions=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

enumCellType{case regular(DataModel)case header(String)}@Publishedvarcells=[CellType]()letcollectionView=UICollectionView()privatevarsubscriptions=Set<AnyCancellable>()collectionView.bind($cells){ collectionView, indexPath, itemsinswitch 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

@PublishedvartableItems=[String]()lettableView=UITableView()privatevarsubscriptions=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:

openclassBaseCell<ViewModel>:UITableViewCell{publicoverrideinit(style:UITableViewCell.CellStyle, reuseIdentifier:String?){        super.init(style: style, reuseIdentifier: reuseIdentifier)commonInit()}publicrequiredinit?(coder:NSCoder){        super.init(coder: coder)commonInit()}openfunc commonInit(){        selectionStyle=.none}openfunc render(viewModel:ViewModel){}}

Subclass Implementation:

finalclassUserCell:BaseCell<UserViewModel>{privateletnameLabel=UILabel()overridefunc commonInit(){        super.commonInit()        contentView.addSubview(nameLabel)}overridefunc render(viewModel:UserViewModel){        nameLabel.text= viewModel.name}}finalclassProductCell:BaseCell<ProductViewModel>{privateletproductLabel=UILabel()overridefunc commonInit(){        super.commonInit()        contentView.addSubview(productLabel)}overridefunc render(viewModel:ProductViewModel){        productLabel.text= viewModel.title}}

Using Subclasses in a Table:

@Publishedvarusers:[UserViewModel]=[]lettableView=UITableView()privatevarsubscriptions=Set<AnyCancellable>()tableView.bind($users, cellType:UserCell.self){ index, user, cellin    cell.render(viewModel: user)}.store(in:&subscriptions)

Installation

Swift Package Manager

Add this to yourPackage.swift file:

dependencies:[.package(url:"https://github.com/KopievDev/CombineCocoa", from:"1.0.0")]

About

Combine+UIKit helpers

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp