{"id":15038223,"url":"https://github.com/devpolant/cellviewmodel","last_synced_at":"2026-03-27T04:07:22.456Z","repository":{"id":56905750,"uuid":"168871840","full_name":"devpolant/CellViewModel","owner":"devpolant","description":"Just one possible approach to manage UITableView and UICollectionView data source","archived":false,"fork":false,"pushed_at":"2021-05-09T09:38:26.000Z","size":75,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-03-15T10:20:29.762Z","etag":null,"topics":["collectionview","collectionviewcell","ios","swift","swift-4","tableview","tableviewcell","viewmodel"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devpolant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-02T19:57:42.000Z","updated_at":"2024-01-11T11:23:01.000Z","dependencies_parsed_at":"2022-08-21T03:20:43.973Z","dependency_job_id":null,"html_url":"https://github.com/devpolant/CellViewModel","commit_stats":null,"previous_names":["antonpoltoratskyi/cellviewmodel"],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devpolant%2FCellViewModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devpolant%2FCellViewModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devpolant%2FCellViewModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devpolant%2FCellViewModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devpolant","download_url":"https://codeload.github.com/devpolant/CellViewModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131466,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["collectionview","collectionviewcell","ios","swift","swift-4","tableview","tableviewcell","viewmodel"],"created_at":"2024-09-24T20:37:35.783Z","updated_at":"2026-03-27T04:07:22.410Z","avatar_url":"https://github.com/devpolant.png","language":"Swift","readme":"[![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)](https://swift.org)\n[![Xcode](https://img.shields.io/badge/Xcode-12.0-blue.svg)](https://developer.apple.com/xcode)\n[![MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://opensource.org/licenses/MIT)\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/CellViewModel.svg)](https://cocoapods.org/pods/CellViewModel)\n\n# CellViewModel\n\nUsing CellViewModel to configure you UITableViewCell or UICollectionViewCell is just a one possible approach of work with UIKit's collections.\n\n## Requirements:\n- iOS 9.0+\n- Xcode 12.0+\n- Swift 5.0+\n\n## Installation\n\n#### CocoaPods\n\n```ruby\ntarget 'MyApp' do\n  pod 'CellViewModel', '~\u003e 1.8.0'\nend\n```\n\n#### Carthage\n\n```ogdl\ngithub \"devpolant/CellViewModel\" \"master\"\n```\n\n## Usage\n\n**Works with UITableView \u0026 UICollectionView** - one possible approach, inspired by **CocoaHeads**:\n\nYou can move configuration logic for **UITableViewCell** or **UICollectionViewCell** from **-cellForRowAtIndexPath:** to separate types.\n\n### Native setup\n\n1) Create cell class and appropriate type that conforms to **CellViewModel** type:\n\n```Swift\npublic typealias AnyViewCell = UIView\n\npublic protocol CellViewModel: AnyCellViewModel {\n    associatedtype Cell: AnyViewCell\n    func setup(cell: Cell)\n}\n```\n\n\u003e **UserTableViewCell.swift**\n\n```Swift\nimport CellViewModel\n\n// MARK: - View Model\n\nstruct UserCellModel: CellViewModel {\n    var user: User\n    \n    func setup(cell: UserTableViewCell) {\n        cell.nameLabel.text = user.name\n    }\n}\n\n// MARK: - Cell\n\nfinal class UserTableViewCell: UITableViewCell, XibInitializable {\n    @IBOutlet weak var nameLabel: UILabel!\n}\n```\n\n2) Register created model type:\n```Swift\ntableView.register(UserCellModel.self)\n```\n\nBy registering model type it will be checked if cell class conforms to XibInitializable or not in order to register `UINib` or just cell's class type.\n\n3) Then store your models in array (or your custom datasource type):\n\n```Swift\nprivate var users: [AnyCellViewModel] = []\n```\n\n**AnyCellViewModel** is a base protocol of **CellViewModel**. \nIt's needed only in order to fix compiler limitation as **you can use protocols with associatedtype only as generic constraints** and can't write something like this:\n\n```Swift\nprivate var users: [CellViewModel] = [] // won't compile\n```\n\n4) **UITableViewDataSource** implementation is very easy, even if you have multiple cell types, because all 'cellForRow' logic is contained in our view models:\n\n```Swift\nimport CellViewModel\n\nclass ViewController: UIViewController {\n    \n    @IBOutlet weak var tableView: UITableView!\n    \n    private var users: [AnyCellViewModel] = []\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        users = User.testDataSource.map { UserCellModel(user: $0) }\n        tableView.register(nibModel: UserCellModel.self)\n    }\n}\n\nextension ViewController: UITableViewDataSource {\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -\u003e Int {\n        return users.count\n    }\n    \n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -\u003e UITableViewCell {\n        return tableView.dequeueReusableCell(with: tableModel(at: indexPath), for: indexPath)\n    }\n    \n    private func tableModel(at indexPath: IndexPath) -\u003e AnyCellViewModel {\n        return users[indexPath.row]\n    }\n}\n```\n\n### Quick Setup\n\nUse existed adapters in order to perform quick setup. \n\n1. For `UITableView` - **TableViewDataAdapter**\n\n```swift\nprivate lazy var adapter = TableViewDataAdapter(tableView: self.tableView)\n\n// ...\n\nfunc setup(users: [AnyCellViewModel]) {\n    adapter.data = users\n}\n```\n\nUpdating `data` property will call `reloadData()`.\n\n2. For `UICollectionView` - **CollectionViewDataAdapter**:\n\n```swift\nprivate lazy var adapter = CollectionViewDataAdapter(tableView: self.collectionView)\n\n// ...\n\nfunc setup(users: [AnyCellViewModel]) {\n    adapter.data = users\n}\n```\n\nBoth adapters already conform to appropriate datasource protocol: `UICollectionViewDataSource` and `UITableViewDataSource`.\n\n### Base View Controller\n\nThe most simplier way to set up is to inherit from `BaseCollectionViewController`.\n\nSometimes you need a table UI, but with some unique section insets or interitem spacing. For this case `BaseCollectionViewController` provides default implementation of `UICollectionViewDelegateFlowLayout` protocol to match the  table UI for you.\n\n#### Usage\n\n```swift\n\nfinal class UsersViewController: BaseCollectionViewController {\n\n    @IBOutlet weak var collectionView: UICollectionView {\n        didSet {\n            // initialize reference in base class\n            _collectionView = collectionView\n        }\n    }\n    \n    override var viewModels: [AnyCellViewModel.Type] {\n        return [\n            UserCellModel.self,\n            // ... add more\n        ]\n    }\n    \n    override var supplementaryModels: [AnySupplementaryViewModel.Type] {\n        return [\n            UserHeaderModel.self,\n            /// ... add more\n        ]\n    }\n    \n    // ... your domain code\n```\n\n`BaseCollectionViewController` is a wrapper for `CollectionViewDataAdapter`, so it is already have setup method:\n\n```swift\n    open func setup(_ sections: [Section]) {\n        adapter.data = sections\n    }\n```\n\n`Section` type is a container for header, footer, items models and layout information like spacings etc.\n\n```swift\npublic final class Section {\n    \n    public var insets: UIEdgeInsets?\n    public var lineSpacing: CGFloat?\n    public var header: AnySupplementaryViewModel?\n    public var footer: AnySupplementaryViewModel?\n    public var items: [AnyCellViewModel]\n    \n    /// ...\n}\n```\n\nOverride `automaticallyInferCellViewModelTypes` in order to allow to automatically infer type of used view models instead of explicitly declare them in `viewModels` and `supplementaryModels` properties.\n\n```swift\noverride var automaticallyInferCellViewModelTypes: Bool {\n    return true\n}\n```\n\n\n### Accessibility\n\nSometimes there is a need to define `accessibilityIdentifier` for UI testing purposes.\n\nThere is [Accessible](https://github.com/devpolant/CellViewModel/blob/master/CellViewModel/Sources/ViewModels/Accessibility/Accessible.swift) protocol that is conformed by CellViewModel protocol.\n\n```swift\npublic protocol Accessible {\n    var accessibilityIdentifier: String? { get }\n    var accessibilityOptions: AccessibilityDisplayOptions { get }\n}\n```\n\nSo you need to define `accessibilityIdentifier` property in your model type implementation:\n\n```swift\nstruct UserCellModel: CellViewModel {\n\n    var accessibilityIdentifier: String? {\n        return \"user_cell\"\n    }\n\n    // ...\n}\n```\n\nAnd define `accessibilityOptions` if needed to add index path as suffix in the end of `accessibilityIdentifier`:\n\n```swift\nstruct UserCellModel: CellViewModel {\n\n    var accessibilityIdentifier: String? {\n        return \"user_cell\"\n    }\n    \n    var accessibilityOptions: AccessibilityDisplayOptions {\n        return [.row, .section]\n    }\n\n    // ...\n}\n```\n\n## License\n\n**CellViewModel** is available under the MIT license. See the [LICENSE](https://github.com/devpolant/CellViewModel/blob/master/LICENSE) file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevpolant%2Fcellviewmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevpolant%2Fcellviewmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevpolant%2Fcellviewmodel/lists"}