• Stars
    star
    3,054
  • Rank 14,099 (Top 0.3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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
  • Supports extending your item and section structures
    • just extend your item with IdentifiableType and Equatable, and your section with AnimatableSectionModelType
  • Supports all combinations of two level hierarchical animations for both 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 for Insert, Reload and Delete (Automatic, Fade, ...)
  • Example app
  • Randomized stress tests (example app)
  • Supports editing out of the box (example app)
  • Works with UITableView and UICollectionView

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(_:_:)
let data = Observable<[String]>.just(["first element", "second element", "third element"])

data.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { index, model, cell in
  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

let dataSource = 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:

struct CustomData {
  var anInt: Int
  var aString: String
  var aCGPoint: CGPoint
}
  1. Start by defining your sections with a struct that conforms to the SectionModelType protocol:
  • define the Item typealias: equal to the type of items that the section will contain
  • declare an items property: of type array of Item
struct SectionOfCustomData {
  var header: String    
  var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
  typealias Item = CustomData

   init(original: SectionOfCustomData, items: [Item]) {
    self = original
    self.items = items
  }
}
  1. Create a dataSource object and pass it your SectionOfCustomData type:
let dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>(
  configureCell: { dataSource, tableView, indexPath, item in
    let cell = 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, index in
  return dataSource.sectionModels[index].header
}

dataSource.titleForFooterInSection = { dataSource, index in
  return dataSource.sectionModels[index].footer
}

dataSource.canEditRowAtIndexPath = { dataSource, indexPath in
  return true
}

dataSource.canMoveRowAtIndexPath = { dataSource, indexPath in
  return true
}
  1. Define the actual data as an Observable sequence of CustomData objects and bind it to the tableView
let sections = [
  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 and RxCollectionViewSectionedAnimatedDataSource.

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 to AnimatableSectionModelType
  • Your data model must conform to
    • IdentifiableType: The identity provided by the IdentifiableType protocol must be an immutable identifier representing an instance of the model. For example, in case of a Car model, you might want to use the car's plateNumber as its identity.
    • Equatable: Conforming to Equatable helps RxDataSources determine which cells have changed so it can animate only these specific cells. Meaning, changing any of the Car 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 versions 3.0.0 ... 3.1.0 For Swift 3.x version please use versions 1.0 ... 2.0.2 For Swift 2.3 version please use versions 0.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 a Package.swift file.

import PackageDescription

let package = 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 to File / Swift Packages / Add Package Dependency... and enter package repository URL https://github.com/RxSwiftCommunity/RxDataSources.git, then follow the instructions.

More Repositories

1

RxFlow

RxFlow is a navigation framework for iOS applications based on a Reactive Flow Coordinator pattern
Swift
1,835
star
2

RxAlamofire

RxSwift wrapper around the elegant HTTP networking in Swift Alamofire
Swift
1,594
star
3

RxKeyboard

Reactive Keyboard in iOS
Swift
1,533
star
4

RxGesture

RxSwift reactive wrapper for view gestures
Swift
1,340
star
5

RxSwiftExt

A collection of Rx operators & tools not found in the core RxSwift distribution
Swift
1,290
star
6

RxRealm

RxSwift extension for RealmSwift's types
Swift
1,133
star
7

Action

Abstracts actions to be performed in RxSwift.
Swift
876
star
8

RxOptional

RxSwift extensions for Swift optionals and "Occupiable" types
Swift
701
star
9

RxAnimated

Animated RxCocoa bindings
Swift
686
star
10

NSObject-Rx

Handy RxSwift extensions on NSObject, including rx.disposeBag.
Swift
640
star
11

RxMarbles

RxMarbles iOS app
Swift
484
star
12

RxViewModel

ReactiveViewModel-esque using RxSwift
Swift
403
star
13

RxTheme

Theme management based on Rx
Swift
381
star
14

RxReachability

RxSwift bindings for Reachability
Swift
283
star
15

RxNimble

Nimble extensions making unit testing with RxSwift easier ๐ŸŽ‰
Swift
265
star
16

RxWebKit

RxWebKit is a RxSwift wrapper for WebKit
Swift
247
star
17

RxFirebase

RxSwift extensions for Firebase
Swift
224
star
18

RxKingfisher

Reactive extension for the Kingfisher image downloading and caching library
Swift
223
star
19

RxGRDB

Reactive extensions for SQLite
Swift
214
star
20

RxSwiftUtilities

Helpful classes and extensions for RxSwift
Swift
188
star
21

RxCoreLocation

RxCoreLocation is a reactive abstraction to manage Core Location.
Swift
180
star
22

RxMediaPicker

A reactive wrapper built around UIImagePickerController.
Swift
179
star
23

RxCoreData

RxSwift extensions for Core Data
C
164
star
24

RxRealmDataSources

An easy way to bind an RxRealm observable to a table or collection view
Swift
161
star
25

RxState

Redux implementation in Swift using RxSwift
Swift
153
star
26

RxStarscream

A lightweight extension to subscribe Starscream websocket events with RxSwift
Swift
151
star
27

RxVisualDebugger

WIP! Very quick and very dirty test for a visual Rx debugger
JavaScript
142
star
28

RxLocalizer

RxLocalizer allows you to localize your apps, using RxSwift ๐Ÿš€
Swift
134
star
29

RxBiBinding

Reactive two-way binding
Swift
127
star
30

RxReduce

RxReduce is a lightweight framework that ease the implementation of a state container pattern in a Reactive Programming compliant way.
Swift
125
star
31

RxMKMapView

RxMKMapView is a RxSwift wrapper for MKMapView `delegate`.
Swift
121
star
32

RxASDataSources

RxDataSource for AsyncDisplayKit/Texture
Swift
118
star
33

RxCocoa-Texture

RxCocoa Extension Library for Texture.
Swift
99
star
34

RxGoogleMaps

RxSwift reactive wrapper for GoogleMaps SDK
Swift
95
star
35

RxSegue

Swift
80
star
36

FirebaseRxSwiftExtensions

Extension Methods for Firebase and RxSwift
Swift
77
star
37

RxMultipeer

A testable RxSwift wrapper around MultipeerConnectivity
Swift
69
star
38

RxIGListKit

RxSwift wrapper for IGListKit
Swift
62
star
39

RxBinding

Simple data binding operators ~> and <~> for RxSwift.
Swift
62
star
40

RxPager

Pager for RxSwift
Swift
60
star
41

RxCoreMotion

Provides an easy and straight-forward way to use Apple iOS CoreMotion responses as Rx Observables.
Swift
60
star
42

RxFileMonitor

RxSwift wrapper around CoreFoundation file events (FSEvent*)
Swift
60
star
43

RxFirebase-Deprecated

Implement RxSwift with the new Firebase
Swift
54
star
44

RxAlert

Swift
51
star
45

RxSnippets

Several snippets for work with RxSwift
50
star
46

RxCookbook

Community driven RxSwift cookbook ๐Ÿด๐Ÿ“š
50
star
47

rxswiftcommunity.github.io

For projects that support RxSwift
Ruby
50
star
48

RxController

A library for developing iOS app with MVVM-C based on RxFlow and RxSwift.
Swift
43
star
49

RxHttpClient

Simple Http client (Use RxSwift for stream data)
Swift
39
star
50

RxEventHub

`RxEventHub` makes multicasting event easy, type-safe and error-free, use it instead of `NSNotificationCenter` now!
Swift
36
star
51

RxModal

Subscribe to your modal flows
Swift
28
star
52

RxBatteryManager

A Reactive BatteryManager in Swift for iOS
Swift
23
star
53

contributors

Guidelines for contributing to the RxSwiftCommunity, and a good place to raise questions.
20
star
54

guides.rxswift.org

Content of the website guides.rxswift.org
HTML
19
star
55

RxAlertViewable

A simple alert library with RxSwift MVVM supported.
Swift
18
star
56

RxAVFoundation

RxAVFoundation (based on RxSwift)
Swift
17
star
57

RxTask

An RxSwift implementation of a command line runner.
Swift
16
star
58

RxContacts

RxContacts is a RxSwift wrapper around the Contacts Framework.
Swift
13
star
59

RxTestExt

A collection of operators & tools not found in the core RxTest distribution
Swift
13
star
60

RxVision

RxVision (based on RxSwift)
Swift
13
star
61

RxCoreNFC

RxCoreNFC (based on RxSwift)
Swift
11
star
62

RxCloudKit

RxCloudKit (based on RxSwift)
Swift
9
star
63

RxARKit

RxARKit (based on RxSwift)
Swift
9
star
64

RxTapAction

Reactive extensions for adding tap action gesture to UIView or UICollectionView.
Swift
9
star
65

RxSceneKit

RxSceneKit (based on RxSwift)
Swift
5
star
66

SimplestDemostrationOfUsingOperator

Simplest way to show how `using` operator works.
Swift
5
star
67

RxSocket.io

Rx wrapper over socket.io library with Generic functions
Swift
5
star
68

Docs

RxSwift Official Docs - Generated by Jazzy
HTML
5
star
69

RxSpriteKit

RxSpriteKit (based on RxSwift)
Swift
4
star
70

RxOnDemandResources

RxOnDemandResources (based on RxSwift)
Swift
4
star
71

FakeRepo

This is a temporary fake repo, please ignore it :)
Swift
2
star
72

peril

Settings for the RxSwiftCommunity organization's Peril server
TypeScript
2
star