Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

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

UITableView and UICollectionView Data Sources for RxSwift (sections, animated updates, editing ...)

License

NotificationsYou must be signed in to change notification settings

RxSwiftCommunity/RxDataSources

Repository files navigation

Travis CI

Table and Collection view data sources

Features

  • O(N) algorithm for calculating differences
    • the algorithm has the assumption that all sections and items are unique so there is no ambiguity
    • in case there is ambiguity, fallbacks automagically on non animated refresh
  • it applies additional heuristics to send the least number of commands to sectioned view
    • even though the running time is linear, preferred number of sent commands is usually a lot less than linear
    • it is preferred (and possible) to cap the number of changes to some small number, and in case the number of changes grows towards linear, just do normal reload
  • Supportsextending your item and section structures
    • just extend your item withIdentifiableType andEquatable, and your section withAnimatableSectionModelType
  • Supports all combinations of two level hierarchical animations forboth sections and items
    • Section animations: Insert, Delete, Move
    • Item animations: Insert, Delete, Move, Reload (if old value is not equal to new value)
  • Configurable animation types forInsert,Reload andDelete (Automatic, Fade, ...)
  • Example app
  • Randomized stress tests (example app)
  • Supports editing out of the box (example app)
  • Works withUITableView andUICollectionView

Why

Writing table and collection view data sources is tedious. There is a large number of delegate methods that need to be implemented for the simplest case possible.

RxSwift helps alleviate some of the burden with a simple data binding mechanism:

  1. Turn your data into an Observable sequence
  2. Bind the data to the tableView/collectionView using one of:
  • rx.items(dataSource:protocol<RxTableViewDataSourceType, UITableViewDataSource>)
  • rx.items(cellIdentifier:String)
  • rx.items(cellIdentifier:String:Cell.Type:_:)
  • rx.items(_:_:)
letdata= Observable<[String]>.just(["first element","second element","third element"])data.bind(to: tableView.rx.items(cellIdentifier:"Cell")){ index, model, cellin  cell.textLabel?.text= model}.disposed(by: disposeBag)

This works well with simple data sets but does not handle cases where you need to bind complex data sets with multiples sections, or when you need to perform animations when adding/modifying/deleting items.

These are precisely the use cases that RxDataSources helps solve.

With RxDataSources, it is super easy to just write

letdataSource=RxTableViewSectionedReloadDataSource<SectionModel<String,Int>>(configureCell: configureCell)Observable.just([SectionModel(model:"title", items:[1,2,3])]).bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)

RxDataSources example app

How

Given the following custom data structure:

structCustomData{varanInt:IntvaraString:StringvaraCGPoint:CGPoint}
  1. Start by defining your sections with a struct that conforms to theSectionModelType protocol:
  • define theItem typealias: equal to the type of items that the section will contain
  • declare anitems property: of type array ofItem
structSectionOfCustomData{varheader:Stringvaritems:[Item]}extensionSectionOfCustomData:SectionModelType{typealiasItem=CustomDatainit(original:SectionOfCustomData, items:[Item]){self= originalself.items= items}}
  1. Create a dataSource object and pass it yourSectionOfCustomData type:
letdataSource=RxTableViewSectionedReloadDataSource<SectionOfCustomData>(  configureCell:{ dataSource, tableView, indexPath, iteminletcell= tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath)    cell.textLabel?.text="Item\(item.anInt):\(item.aString) -\(item.aCGPoint.x):\(item.aCGPoint.y)"return cell})
  1. Customize closures on the dataSource as needed:
  • titleForHeaderInSection
  • titleForFooterInSection
  • etc
dataSource.titleForHeaderInSection={ dataSource, indexinreturn dataSource.sectionModels[index].header}dataSource.titleForFooterInSection={ dataSource, indexinreturn dataSource.sectionModels[index].footer}dataSource.canEditRowAtIndexPath={ dataSource, indexPathinreturntrue}dataSource.canMoveRowAtIndexPath={ dataSource, indexPathinreturntrue}
  1. Define the actual data as an Observable sequence of CustomData objects and bind it to the tableView
letsections=[SectionOfCustomData(header:"First section", items:[CustomData(anInt:0, aString:"zero", aCGPoint:CGPoint.zero),CustomData(anInt:1, aString:"one", aCGPoint:CGPoint(x:1, y:1))]),SectionOfCustomData(header:"Second section", items:[CustomData(anInt:2, aString:"two", aCGPoint:CGPoint(x:2, y:2)),CustomData(anInt:3, aString:"three", aCGPoint:CGPoint(x:3, y:3))])]Observable.just(sections).bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)

Animated Data Sources

RxDataSources provides two special data source types that automatically take care of animating changes in the bound data source:RxTableViewSectionedAnimatedDataSource andRxCollectionViewSectionedAnimatedDataSource.

To use one of the two animated data sources, you must take a few extra steps on top of those outlined above:

  • SectionOfCustomData needs to conform toAnimatableSectionModelType
  • Your data model must conform to
    • IdentifiableType: Theidentity provided by theIdentifiableType protocol must be animmutable identifier representing an instance of the model. For example, in case of aCar model, you might want to use the car'splateNumber as its identity.
    • Equatable: Conforming toEquatable helpsRxDataSources determine which cells have changed so it can animate only these specific cells. Meaning, changingany of theCar model's properties will trigger an animated reload of that cell.

Requirements

Xcode 10.2

Swift 5.0

For Swift 4.x version please use versions3.0.0 ... 3.1.0For Swift 3.x version please use versions1.0 ... 2.0.2For Swift 2.3 version please use versions0.1 ... 0.9

Installation

We'll try to keep the API as stable as possible, but breaking API changes can occur.

CocoaPods

Podfile

pod 'RxDataSources', '~> 5.0'

Carthage

Cartfile

github "RxSwiftCommunity/RxDataSources" ~> 5.0

Swift Package Manager

Create aPackage.swift file.

import PackageDescriptionletpackage=Package(    name:"SampleProject",    dependencies:[.package(url:"https://github.com/RxSwiftCommunity/RxDataSources.git", from:"5.0.0")])

If you are using Xcode 11 or higher, go toFile / Swift Packages / Add Package Dependency... and enter package repository URLhttps://github.com/RxSwiftCommunity/RxDataSources.git, then follow the instructions.


[8]ページ先頭

©2009-2025 Movatter.jp