Coder Social home page Coder Social logo

imjerrybao / rxdatasources Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rxswiftcommunity/rxdatasources

0.0 2.0 0.0 1.41 MB

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

License: MIT License

Swift 95.35% Ruby 1.29% Objective-C 0.36% Shell 2.99%

rxdatasources's Introduction

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 then 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>>()
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>()
  1. Customize closures on the dataSource as needed:
  • configureCell (required)
  • titleForHeaderInSection
  • titleForFooterInSection
  • etc
dataSource.configureCell = { (ds: RxTableViewSectionedReloadDataSource<SectionOfCustomData>, tv: UITableView, ip: IndexPath, item: Item) in
  let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: ip)
  cell.textLabel?.text = "Item \(item.anInt): \(item.aString) - \(item.aCGPoint.x):\(item.aCGPoint.y)"
  return cell
}
dataSource.titleForHeaderInSection = { ds, index in
  return ds.sectionModels[index].header
}
  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)

Animations

To implement animations with RxDataSources, the same steps are required as with non-animated data, execept:

  • SectionOfCustomData needs to conform to AnimatableSectionModelType
  • dataSource needs to be an instance of RxTableViewSectionedAnimatedDataSource or RxCollectionViewSectionedAnimatedDataSource

Requirements

Xcode 8.0 GM (8A218a)

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', '~> 1.0'

Carthage

Cartfile

github "RxSwiftCommunity/RxDataSources" ~> 1.0

rxdatasources's People

Contributors

coryoso avatar darrensapalo avatar esttorhe avatar igorkulman avatar inkyfox avatar joehetfield avatar kzaher avatar lailore avatar leonerd42 avatar mustafaibrahim989 avatar orta avatar riccardo-tesio avatar sergdort avatar serluca avatar sger avatar shams-ahmed avatar solidcell avatar tcurdt avatar tomburns avatar touyu avatar urosk avatar vhbit avatar vinceyuan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.