RxDataSources

repository·main·Indexed 25 days ago

https://github.com/rxswiftcommunity/rxdatasources

Advanced data source implementations for UITableView and UICollectionView using RxSwift. It supports complex, multi-section data binding and high-performance O(N) animated updates via RxTableViewSectionedAnimatedDataSource and RxCollectionViewSectionedAnimatedDataSource.

Tokens
1.3K
Snippets
2
Records
4
Agent score
35%

What's inside RxDataSources

  1. Install RxDataSources

    main

    You can install RxDataSources using CocoaPods, Carthage, or Swift Package Manager.

    ### CocoaPods
    ```ruby
    pod 'RxDataSources', '~> 5.0'

    Carthage

    gituh "RxSwiftCommunity/RxDataSources" ~> 5.0

    Swift Package Manager

    import PackageDescription
    
    let package = Package(
        name: "SampleProject",
        dependencies: [
            .package(url: "https://github.com/RxSwiftCommunity/RxDataSources.git", from: "5.0.0")
        ]
    )
  2. Implement a non-animated TableView data source

    main

    To use a non-animated data source like RxTableViewSectionedReloadDataSource, follow these steps:

    1. Define your section model: Create a struct that conforms to SectionModelType. It must define a typealias Item and an items property of type [Item]. The struct must also implement an initializer init(original: SectionModelType, items: [Item]).
    2. Create the data source: Instantiate the desired data source type (e.g., RxTableViewSectionedReloadDataSource) and provide a configureCell closure.
    3. Customize closures: Optionally set properties like titleForHeaderInSection, titleForFooterInSection, canEditRowAtIndexPath, or canMoveRowAtIndexPath.
    4. Bind to TableView: Create an Observable sequence of your section models and bind it to tableView.rx.items(dataSource: dataSource).
    struct CustomData {
      var anInt: Int
      var aString: String
      var aCGPoint: CGPoint
    }
    
    struct SectionOfCustomData {
      var header: String    
      var items: [Item]
    }
    extension SectionOfCustomData: SectionModelType {
      typealias Item = CustomData
    
       init(original: SectionOfCustomData, items: [Item]) {
        self = original
        self.items = items
      }
    }
    
    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
    })
    
    // Customization example
    dataSource.titleForHeaderInSection = { dataSource, index in
      return dataSource.sectionModels[index].header
    }
    
    // Binding
    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)
  3. Implement animated TableView or CollectionView data sources

    main

    To use RxTableViewSectionedAnimatedDataSource or RxCollectionViewSectionedAnimatedDataSource for automatic animations, your data structures must meet these requirements:

    1. Section Model: Must conform to AnimatableSectionModelType.
    2. Item Model: Must conform to:
      • IdentifiableType: The identity must be an immutable identifier representing a unique instance (e.g., a unique ID or plate number).
      • Equatable: Used to determine if a cell needs a reload animation. Changing any property that affects equality will trigger an animated reload of that cell.