All Projects → GenericDataSource → Genericdatasource

GenericDataSource / Genericdatasource

Licence: mit
A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Genericdatasource

Closures
Swifty closures for UIKit and Foundation
Stars: ✭ 1,720 (+1254.33%)
Mutual labels:  datasource, uicollectionview, uitableview
Tangramkit
TangramKit is a powerful iOS UI framework implemented by Swift. It integrates the functions with Android layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICollectionView
Stars: ✭ 984 (+674.8%)
Mutual labels:  xcode, uicollectionview, uitableview
Gltablecollectionview
Netflix and App Store like UITableView with UICollectionView, written in pure Swift 4.2
Stars: ✭ 709 (+458.27%)
Mutual labels:  xcode, uicollectionview, uitableview
iOSEasyList
A data-driven UICollectionView and UITableView framework for building fast and flexible lists
Stars: ✭ 29 (-77.17%)
Mutual labels:  uitableview, uicollectionview, datasource
Carbon
🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
Stars: ✭ 1,034 (+714.17%)
Mutual labels:  datasource, uicollectionview, uitableview
Rglistkit
RGListKit is a Protocol & MVVM based framework to easily populate a UITableView or UICollectionView via single api.
Stars: ✭ 178 (+40.16%)
Mutual labels:  datasource, uicollectionview, uitableview
Thinningcoordinator
The UITableView/UICollectionView dataSource/delegate thinning coordinator, help thinning your UIViewController!
Stars: ✭ 25 (-80.31%)
Mutual labels:  datasource, uicollectionview, uitableview
Flowkit
A declarative type-safe framework for building fast and flexible list with Tables & Collection
Stars: ✭ 215 (+69.29%)
Mutual labels:  datasource, uicollectionview, uitableview
TinyCoordinator
The Swift version of ThinningCoordinator focus on lighter view controllers.
Stars: ✭ 18 (-85.83%)
Mutual labels:  uitableview, uicollectionview, datasource
Mylinearlayout
MyLayout is a powerful iOS UI framework implemented by Objective-C. It integrates the functions with Android Layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,PathLayout,GridLayout,LayoutSizeClass to build your App 自动布局 UIView UITab…
Stars: ✭ 4,152 (+3169.29%)
Mutual labels:  xcode, uicollectionview, uitableview
Flix
iOS reusable form library in Swift.
Stars: ✭ 725 (+470.87%)
Mutual labels:  uicollectionview, uitableview
Tysnapshotscroll
一句代码保存截图,将 UIScrollView UITableView UICollectionView UIWebView WKWebView 网页 保存 为 长图 查看。Save the scroll view page as an image,support UIScrollView,UITableView,UICollectionView,UIWebView,WKWebView.(Support iOS13)
Stars: ✭ 709 (+458.27%)
Mutual labels:  uicollectionview, uitableview
Viewanimator
ViewAnimator brings your UI to life with just one line
Stars: ✭ 6,592 (+5090.55%)
Mutual labels:  uicollectionview, uitableview
Changeset
Minimal edits from one collection to another
Stars: ✭ 807 (+535.43%)
Mutual labels:  uicollectionview, uitableview
Gravityslider
🔄 GravitySlider is a beautiful alternative to the standard UICollectionView flow layout.
Stars: ✭ 784 (+517.32%)
Mutual labels:  xcode, uicollectionview
Tablekit
Type-safe declarative table views.
Stars: ✭ 567 (+346.46%)
Mutual labels:  generic, uitableview
Datasources
💾 🔜📱 Type-safe data-driven CollectionView, TableView Framework. (We can also use ASCollectionNode)
Stars: ✭ 553 (+335.43%)
Mutual labels:  datasource, uicollectionview
Cascadingtabledelegate
A no-nonsense way to write cleaner UITableViewDelegate and UITableViewDataSource in Swift.
Stars: ✭ 931 (+633.07%)
Mutual labels:  clean-code, uitableview
Viper Templates
Swift Xcode templates for creating VIPER architecture stacks
Stars: ✭ 33 (-74.02%)
Mutual labels:  xcode, clean-code
Datasource
Simplifies the setup of UITableView data sources using type-safe descriptors for cells and sections. Animated diffing built-in.
Stars: ✭ 72 (-43.31%)
Mutual labels:  datasource, uitableview

GenericDataSource

Swift Version

Version

Build Status Coverage Status Documentation

A generic small reusable components for data source implementation for UITableView/UICollectionView written in Swift.

Features

  • [x] BasicDataSource easily bind model to cells with automatic dequeuing.
  • [x] SegmentedDataSource easily build segmented controls or for empty state of your UICollectionView/UITableView data source.
  • [x] CompositeDataSource builds complex cells/models structure with easy to use components (BasicDataSource SegmentedDataSource or other CompositeDataSource).
  • [x] UICollectionView supplementary, UITableView header, and footer views support.
  • [x] Ability to override any data source method from UIKit classes.
  • [x] Comprehensive Unit Test Coverage.
  • [x] Complete Documentation

Requirements

  • iOS 8.0+
  • Xcode 10
  • Swift 4.0+

Installation

CocoaPods

To integrate GenericDataSource into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'GenericDataSources'

IMPORTANT: The pod name is GenericDataSources with "s" at the end.

Carthage

To integrate GenericDataSource into your Xcode project using Carthage, specify it in your Cartfile:

github "GenericDataSource/GenericDataSource"

Manually

Add GenericDataSource.xcodeproj to your project file by drag and drop.

You can then consult to Adding an Existing Framework to a Project.


Example

Basic Data Source Example

UITableView

Create a basic data source and bind it to to a table view.

let dataSource = BasicBlockDataSource<Example, BasicTableViewCell>() { (item: Example, cell: BasicTableViewCell, indexPath) -> Void in
    cell.titleLabel?.text = item.title
}

// Need to keep a strong reference to our data source.
self.dataSource = dataSource

// register the cell
tableView.ds_register(cellClass: BasicTableViewCell.self)
// bind the data source to the table view
tableView.ds_useDataSource(dataSource)

dataSource.items =  <<retrieve items>> // Can be set and altered at anytime

That's it! Your first data source is implemented. No dequeuing! no casting! simple and smart.

UICollectionView

Let's now take it to the next level. Suppose after we implemented it, the requirements changed and we need to implement it using UICollectionView.

let dataSource = BasicBlockDataSource<Example, BasicCollectionViewCell>() { (item: Example, cell: BasicCollectionViewCell, indexPath) -> Void in
    cell.titleLabel?.text = item.title
}

// Need to keep a strong reference to our data source.
self.dataSource = dataSource

// register the cell
collectionView.ds_register(cellClass: BasicCollectionViewCell.self)
// bind the data source to the collection view
collectionView.ds_useDataSource(dataSource)

dataSource.items =  <<retrieve items>> // Can be set and altered at anytime

All you need to do is change the cell class and of course the table view to collection view.

Actually this opens the door for so much possibilities. You can inherit from BasicDataSource and implement your custom generic data source that is based on a protocol implemented by the cell and you don't need to repeat the configuration part. You would create data source like that.

let dataSource1 = CustomDataSource<BasicTableViewCell>() // for table view
let dataSource2 = CustomDataSource<BasicCollectionViewCell>() // for collection view

App store Featured Example

Suppose we want to implement the following screen, the App Store featured tab.

App Store Example Screenshot

If you want to have a look at the complete source code, it is under Example project -> AppStoreViewController.swift.

  1. We will create cells as we do normally.
  2. Now we need to think about DataSources.
  3. It's simple, one data source for each cell type (BasicDataSource).
  4. CompositeDataSource(sectionType: .single) for the table view rows. Since these rows are of different cell types.
  5. SegmentedDataSource for switching between loading and data views.
  6. Bind the SegmentedDataSource data source to the table view and that's it.
  7. See how we think structurally about our UI and data sources instead of one big cell.

One thing we didn't talk about is the UICollectionView of the featured section cells. It's very simple, just BasicDataSource.

See how we can implement the screen in the following code:

  1. Create the cells.
class AppStoreFeaturedSectionTableViewCell: UITableViewCell { ... }
class AppStoreQuickLinkLabelTableViewCell: UITableViewCell { ... }
class AppStoreQuickLinkTableViewCell: UITableViewCell { ... }
class AppStoreFooterTableViewCell: UITableViewCell { ... }
class AppStoreLoadingTableViewCell: UITableViewCell { ... }
  1. Create BasicDataSources.
class AppStoreLoadingDataSource: BasicDataSource<Void, AppStoreLoadingTableViewCell> {
    // loading should take full screen size.
    override func ds_collectionView(_ collectionView: GeneralCollectionView, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.size
    }
}
class AppStoreFooterDataSource: BasicDataSource<Void, AppStoreFooterTableViewCell> { ... }
class AppStoreQuickLinkDataSource: BasicDataSource<FeaturedQuickLink, AppStoreQuickLinkTableViewCell> { ...  }
class AppStoreFeaturedAppsDataSource: BasicDataSource<FeaturedApp, AppStoreFeaturedAppCollectionViewCell> { ... }
class AppStoreFeaturedAppsSectionDataSource: BasicDataSource<FeaturedSection, AppStoreFeaturedSectionTableViewCell> { ... }
class AppStoreQuickLinkLabelDataSource: BasicDataSource<String, AppStoreQuickLinkLabelTableViewCell> { ... }
  1. Create CompositeDataSource that holds the featured page.
class AppStoreFeaturedPageDataSource: CompositeDataSource {
    init() { super.init(sectionType: .single)] }

    var page: FeaturedPage? {
        didSet {
            // remove all existing data sources
            removeAllDataSources()

            guard let page = page else {
                return
            }

            // add featured apps
            let featuredApps = AppStoreFeaturedAppsSectionDataSource()
            featuredApps.setSelectionHandler(UnselectableSelectionHandler())
            featuredApps.items = page.sections
            add(featuredApps)

            // add quick link label
            let quickLinkLabel = AppStoreQuickLinkLabelDataSource()
            quickLinkLabel.setSelectionHandler(UnselectableSelectionHandler())
            quickLinkLabel.items = [page.quickLinkLabel]
            add(quickLinkLabel)

            // add quick links
            let quickLinks = AppStoreQuickLinkDataSource()
            quickLinks.items = page.quickLinks
            add(quickLinks)

            // add footer
            let footer = AppStoreFooterDataSource()
            footer.setSelectionHandler(UnselectableSelectionHandler())
            footer.items = [Void()] // we add 1 element to show the footer, 2 elements will show it twice. 0 will not show it.
            add(footer)
        }
    }
}
  1. Create the outer most data source.
class AppStoreDataSource: SegmentedDataSource {

    let loading = AppStoreLoadingDataSource()
    let page = AppStoreFeaturedPageDataSource()

    // reload data on index change
    override var selectedDataSourceIndex: Int {
        didSet {
            ds_reusableViewDelegate?.ds_reloadData()
        }
    }

    override init() {
        super.init()
        loading.items = [Void()] // we add 1 element to show the loading, 2 elements will show it twice. 0 will not show it.

        add(loading)
        add(page)
    }
}
  1. Register cells.
tableView.ds_register(cellNib: AppStoreFeaturedSectionTableViewCell.self)
tableView.ds_register(cellNib: AppStoreQuickLinkLabelTableViewCell.self)
tableView.ds_register(cellNib: AppStoreQuickLinkTableViewCell.self)
tableView.ds_register(cellNib: AppStoreFooterTableViewCell.self)
tableView.ds_register(cellNib: AppStoreLoadingTableViewCell.self)
  1. Set data sources to the collection view.
tableView.ds_useDataSource(dataSource)
  1. Finally set the data when it is available.
  // show loading indicator
  dataSource.selectedDataSourceIndex = 0

  // get the data from the service
  service.getFeaturedPage { [weak self] page in

    // update the data source model
    self?.dataSource.page.page = page

    // show the page
    self?.dataSource.selectedDataSourceIndex = 1
}

There are many benefits of doing that:

  1. Lightweight view controllers.
  2. You don't need to think about indexes anymore, all is handled for us. Only think about how you can structure your cells into smaller data sources.
  3. We can switch between UITableView and UICollectionView without touching data sources or models. Only change the cells to inherit from UITableViewCell or UICollectionViewCell and everything else works.
  4. We can add/delete/update cells easily. For example we decided to add more blue links. We can do it by just adding new item to the array passed to the data source.
  5. We can re-arrange cells as we want. Just move around the add of data sources calls.
  6. Most importantly no if/else in our code.

Check the Examples application for complete implementations.

Attribution

The main idea comes from [WWDC 2014 Advanced User Interfaces with Collection Views] (https://developer.apple.com/videos/play/wwdc2014/232/) written in swift with generics.

Author

Mohamed Afifi, [email protected]

License

GenericDataSource is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].