### RxCollectionViewSectionedAnimatedDataSource Setup for UICollectionView with Animations Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Illustrates setting up RxCollectionViewSectionedAnimatedDataSource for animated updates in UICollectionView. This data source utilizes Differentiator for automatic diff-based animations. It requires UIKit, RxSwift, RxCocoa, RxDataSources, and Differentiator. The example defines identifiable data models and section types, configures cell and supplementary view creation, specifies animation types, and binds observable sections to the collection view. ```swift import UIKit import RxSwift import RxCocoa import RxDataSources import Differentiator // Define an identifiable item type for cells struct Photo: IdentifiableType, Equatable { let id: String let url: String // Required for IdentifiableType var identity: String { return id } } // Define a section model type struct PhotoSection { var title: String var items: [Photo] } // Extend PhotoSection to conform to AnimatableSectionModelType extension PhotoSection: AnimatableSectionModelType { typealias Item = Photo // Specify the item type // Required for AnimatableSectionModelType var identity: String { return title } // Required for AnimatableSectionModelType: Initializer to create a new section from an old one and new items init(original: PhotoSection, items: [Photo]) { self = original self.items = items } } // Assume PhotoCell and SectionHeaderView are custom UICollectionViewCell and UICollectionReusableView subclasses // class PhotoCell: UICollectionViewCell { func configure(with photo: Photo) { /* ... */ } } // class SectionHeaderView: UICollectionReusableView { @IBOutlet var titleLabel: UILabel! } class AnimatedCollectionViewController: UIViewController { @IBOutlet var collectionView: UICollectionView! // Assume collectionView is connected via IBOutlet let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() // Call super first // Create animated data source let dataSource = RxCollectionViewSectionedAnimatedDataSource( configureCell: { dataSource, collectionView, indexPath, photo in // Dequeue and configure cell let cell = collectionView.dequeueReusableCell( withReuseIdentifier: "PhotoCell", // Ensure identifier is set for: indexPath ) as! PhotoCell // Cast to your custom cell type cell.configure(with: photo) // Configure cell with photo data return cell }, configureSupplementaryView: { dataSource, collectionView, kind, indexPath in // Dequeue and configure supplementary view (e.g., header) let header = collectionView.dequeueReusableSupplementaryView( ofKind: kind, withReuseIdentifier: "Header", // Ensure identifier is set for: indexPath ) as! SectionHeaderView // Cast to your custom header type header.titleLabel.text = dataSource.sectionModels[indexPath.section].title // Set header title return header } ) // Configure animations for insertion, reloading, and deletion dataSource.animationConfiguration = AnimationConfiguration( insertAnimation: .fade, // Use fade animation for insertions reloadAnimation: .fade, // Use fade animation for reloads deleteAnimation: .fade // Use fade animation for deletions ) // Create an observable sequence of sections using a helper function let sections = photoSectionsObservable() // Bind the observable sections to the collection view with automatic animations sections .bind(to: collectionView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) } // Helper function to generate an observable of photo sections func photoSectionsObservable() -> Observable<[PhotoSection]> { return Observable.just([ PhotoSection(title: "Recent", items: [ Photo(id: "1", url: "photo1.jpg"), Photo(id: "2", url: "photo2.jpg") ]), PhotoSection(title: "Favorites", items: [ Photo(id: "3", url: "photo3.jpg") ]) ]) } } ``` -------------------------------- ### Swift Package Manager Setup for RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Configure your project to use RxDataSources via Swift Package Manager. This involves creating a Package.swift file or using Xcode's interface to add the package dependency. ```swift import PackageDescription let package = Package( name: "SampleProject", dependencies: [ .package(url: "https://github.com/RxSwiftCommunity/RxDataSources.git", from: "5.0.0") ] ) ``` -------------------------------- ### RxCollectionViewSectionedReloadDataSource Setup for UICollectionView Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Demonstrates how to set up RxCollectionViewSectionedReloadDataSource for a UICollectionView. This data source performs a simple reloadData on changes. It requires UIKit, RxSwift, RxCocoa, and RxDataSources. The code configures cell and supplementary view creation and binds an observable sequence of section models to the collection view. ```swift import UIKit import RxSwift import RxCocoa import RxDataSources class SimpleCollectionViewController: UIViewController { @IBOutlet var collectionView: UICollectionView! // Assume collectionView is connected via IBOutlet let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() // Call super first // Create data source let dataSource = RxCollectionViewSectionedReloadDataSource>( // Specify section and item types configureCell: { dataSource, collectionView, indexPath, item in // Dequeue and configure cell let cell = collectionView.dequeueReusableCell( withReuseIdentifier: "Cell", // Ensure this identifier is set in Storyboard/XIB for: indexPath ) // Configure cell with item data, e.g., cell.textLabel?.text = "(item)" return cell }, configureSupplementaryView: { dataSource, collectionView, kind, indexPath in // Dequeue and configure supplementary view (e.g., header) let header = collectionView.dequeueReusableSupplementaryView( ofKind: kind, withReuseIdentifier: "Header", // Ensure this identifier is set for: indexPath ) // Configure header, e.g., header.titleLabel.text = dataSource.sectionModels[indexPath.section].model return header } ) // Example: Enable moving items (optional) dataSource.canMoveItemAtIndexPath = { dataSource, indexPath in return true } // Create an observable sequence of section models let sections = Observable.just([ SectionModel(model: "Section 1", items: [1, 2, 3, 4]), SectionModel(model: "Section 2", items: [5, 6, 7, 8]) ]) // Bind the observable sections to the collection view using the data source sections .bind(to: collectionView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) } } ``` -------------------------------- ### Define Custom Data Structure for RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Define a custom data structure to be used within your sections. This example shows a simple struct with an integer, a string, and a CGPoint. ```swift struct CustomData { var anInt: Int var aString: String var aCGPoint: CGPoint } ``` -------------------------------- ### Combine RxDataSources with UITableView Delegate and RxCocoa (Swift) Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Shows how to integrate RxTableViewSectionedAnimatedDataSource with UIKit's UITableViewDelegate and RxCocoa for a reactive data binding experience. This example demonstrates binding data, setting the delegate, and handling UI events like item selection and deletion using RxCocoa observables. ```swift import UIKit import RxSwift import RxCocoa import RxDataSources class CustomTableViewController: UIViewController { @IBOutlet var tableView: UITableView! let disposeBag = DisposeBag() var dataSource: RxTableViewSectionedAnimatedDataSource? override func viewDidLoad() { super.viewDidLoad() let dataSource = RxTableViewSectionedAnimatedDataSource( configureCell: { ds, tv, indexPath, item in let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Item \(item)" return cell } ) self.dataSource = dataSource let sections = Observable.just([ MySection(header: "First", items: [1, 2, 3]), MySection(header: "Second", items: [4, 5, 6]) ]) sections .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) // Set delegate for additional customization tableView.rx.setDelegate(self) .disposed(by: disposeBag) // Handle item selection tableView.rx.itemSelected .subscribe(onNext: { [weak self] indexPath in guard let item = self?.dataSource?[indexPath] else { return } print("Selected: \(item)") self?.tableView.deselectRow(at: indexPath, animated: true) }) .disposed(by: disposeBag) // Handle item deletion tableView.rx.itemDeleted .subscribe(onNext: { indexPath in print("Deleted item at \(indexPath)") }) .disposed(by: disposeBag) // Handle item moved tableView.rx.itemMoved .subscribe(onNext: { sourceIndexPath, destinationIndexPath in print("Moved from \(sourceIndexPath) to \(destinationIndexPath)") }) .disposed(by: disposeBag) } } extension CustomTableViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { guard let item = dataSource?[indexPath] else { return 44.0 } return CGFloat(50 + item * 5) } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 40.0 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let header = UIView() header.backgroundColor = .systemGray5 let label = UILabel() label.text = dataSource?.sectionModels[section].header label.frame = CGRect(x: 15, y: 0, width: tableView.bounds.width - 30, height: 40) header.addSubview(label) return header } func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { return .delete } } ``` -------------------------------- ### IdentifiableType Protocol for RxDataSource Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Explains and demonstrates the IdentifiableType protocol, which is crucial for the diff algorithm in RxDataSource. It ensures each item has a unique identity for tracking changes efficiently. Examples include UUID, composite, and integer identities. ```swift import Differentiator // Simple identity with UUID struct Message: IdentifiableType, Equatable { let id: UUID var text: String var timestamp: Date var identity: UUID { return id } } // Composite identity struct Comment: IdentifiableType, Equatable { let postId: String let userId: String let commentId: String var content: String var identity: String { return "\(postId)-\(userId)-\(commentId)" } } // Integer identity struct Item: IdentifiableType, Equatable { let itemId: Int var name: String var identity: Int { return itemId } } // Built-in types that already conform to IdentifiableType: // - Int (identity is self) // - String (identity is self) // - Float, Double (identity is self) let intItems: [Int] = [1, 2, 3] // Already identifiable let stringItems: [String] = ["a", "b", "c"] // Already identifiable ``` -------------------------------- ### RxTableViewSectionedReloadDataSource for UITableView (Swift) Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Implements a `RxTableViewSectionedReloadDataSource` for a `UITableView` using RxSwift. This example shows how to configure cell appearance, header/footer titles, and enable row editing and moving. It binds an observable sequence of `SectionModel`s to the table view, triggering a `reloadData` on data changes without animations. ```swift import UIKit import RxSwift import RxCocoa import RxDataSources import Differentiator class SimpleTableViewController: UIViewController { @IBOutlet var tableView: UITableView! let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() // Create data source with cell configuration let dataSource = RxTableViewSectionedReloadDataSource>( configureCell: { dataSource, tableView, indexPath, item in let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Item \(item)" return cell } ) // Configure header titles dataSource.titleForHeaderInSection = { dataSource, index in return dataSource.sectionModels[index].model } // Configure footer titles dataSource.titleForFooterInSection = { dataSource, index in return "Section \(index) footer" } // Enable editing dataSource.canEditRowAtIndexPath = { dataSource, indexPath in return true } // Enable moving dataSource.canMoveRowAtIndexPath = { dataSource, indexPath in return true } // Create data let sections = Observable.just([ SectionModel(model: "First", items: [1, 2, 3]), SectionModel(model: "Second", items: [4, 5, 6]) ]) // Bind to table view sections .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) } } ``` -------------------------------- ### Carthage Dependency for RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Integrate RxDataSources into your project using Carthage by adding the repository and version to your Cartfile. ```plaintext github "RxSwiftCommunity/RxDataSources" ~> 5.0 ``` -------------------------------- ### Use Built-in SectionModel with RxDataSources (Swift) Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Demonstrates the usage of the generic `SectionModel` struct provided by RxDataSources. This struct simplifies the creation of sectioned data for use with reactive data sources. It can be used with various item types, including basic types like Int and String, as well as custom objects like the `User` struct, showcasing its flexibility. ```swift import Differentiator import RxSwift import RxCocoa // Using the built-in SectionModel let sections = [ SectionModel(model: "First Section", items: [1, 2, 3]), SectionModel(model: "Second Section", items: [4, 5, 6]) ] // Create observable of sections let observable = Observable.just(sections) // The model parameter can be any type let stringSections = [ SectionModel(model: "Section Title", items: ["Apple", "Banana", "Cherry"]) ] // Use with custom types struct User { let name: String let age: Int } let userSections = [ SectionModel(model: "Team A", items: [ User(name: "Alice", age: 30), User(name: "Bob", age: 25) ]), SectionModel(model: "Team B", items: [ User(name: "Charlie", age: 35) ]) ] ``` -------------------------------- ### RxDataSource Diff Algorithm for Sectioned Views Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Illustrates the core diffing algorithm provided by RxDataSource to calculate changesets between two states of sectioned data. It handles deletions, insertions, moves, and updates of both sections and items, enabling animated UI refreshes. ```swift import Differentiator // Define your animatable sections and items struct User: IdentifiableType, Equatable { let id: String var name: String var identity: String { return id } } struct UserSection: AnimatableSectionModelType { var header: String var items: [User] var identity: String { return header } init(original: UserSection, items: [User]) { self = original self.items = items } } // Initial state let oldSections = [ UserSection(header: "Team A", items: [ User(id: "1", name: "Alice"), User(id: "2", name: "Bob") ]), UserSection(header: "Team B", items: [ User(id: "3", name: "Charlie") ]) ] // New state let newSections = [ UserSection(header: "Team A", items: [ User(id: "1", name: "Alice Updated"), User(id: "4", name: "David") ]), UserSection(header: "Team B", items: [ User(id: "3", name: "Charlie"), User(id: "2", name: "Bob") ]) ] // Calculate differences do { let changesets = try Diff.differencesForSectionedView( initialSections: oldSections, finalSections: newSections ) // changesets contains: // - Deleted sections and items // - Inserted sections and items // - Moved sections and items // - Updated items (same identity but different content) for changeset in changesets { print("Deleted sections: \(changeset.deletedSections)") print("Inserted sections: \(changeset.insertedSections)") print("Moved sections: \(changeset.movedSections)") print("Deleted items: \(changeset.deletedItems)") print("Inserted items: \(changeset.insertedItems)") print("Updated items: \(changeset.updatedItems)") print("Moved items: \(changeset.movedItems)") } } catch { print("Error calculating differences: \(error)") // Fallback to reload } ``` -------------------------------- ### CocoaPods Dependency for RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Add RxDataSources as a dependency to your project using CocoaPods. Specify the desired version range in your Podfile. ```ruby pod 'RxDataSources', '~> 5.0' ``` -------------------------------- ### Create RxTableViewSectionedReloadDataSource with Custom Data Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Set up a table view data source using RxTableViewSectionedReloadDataSource. This involves defining a struct that conforms to SectionModelType and then creating the data source instance, configuring cell creation. ```swift 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(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 }) ``` -------------------------------- ### RxTableViewSectionedAnimatedDataSource for UITableView Animations in Swift Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Implements an animated data source for UITableView using RxDataSources in Swift. This allows for diff-based animations when section or item data changes. It requires a UITableView, RxSwift, RxCocoa, and RxDataSources. The `configureCell` closure defines how cells are populated, and `animationConfiguration` customizes the animation effects. ```swift import UIKit import RxSwift import RxCocoa import RxDataSources import Differentiator class AnimatedTableViewController: UIViewController { @IBOutlet var tableView: UITableView! let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() // ... (rest of the code as provided) tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // Create animated data source let dataSource = RxTableViewSectionedAnimatedDataSource( configureCell: { ds, tv, indexPath, item in let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell") cell.textLabel?.text = "Item \(item)" return cell }, titleForHeaderInSection: { ds, index in return ds.sectionModels[index].header } ) // Configure animation style dataSource.animationConfiguration = AnimationConfiguration( insertAnimation: .fade, reloadAnimation: .automatic, deleteAnimation: .left ) // Create initial sections let initialSections = [ MySection(header: "First section", items: [1, 2]), MySection(header: "Second section", items: [3, 4]) ] // Create observable that emits changing data let sections = Observable.interval(.seconds(3), scheduler: MainScheduler.instance) .scan(initialSections) { currentSections, _ in // Simulate data changes - add/remove/modify items var newSections = currentSections if newSections[0].items.count < 5 { newSections[0].items.append(newSections[0].items.count + 1) } else { newSections[0].items.removeFirst() } return newSections } .startWith(initialSections) // Bind with automatic animations sections .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) } } ``` -------------------------------- ### AnimatableSectionModel for RxDataSource Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Demonstrates the usage of AnimatableSectionModel for organizing data into sections, suitable for use with RxDataSource. It supports both built-in types and custom identifiable types, facilitating animated updates in table views. ```swift import Differentiator // Simple usage with built-in types (Int, String conform to IdentifiableType) let sections = [ AnimatableSectionModel(model: "Numbers", items: [1, 2, 3]), AnimatableSectionModel(model: "More Numbers", items: [4, 5, 6]) ] // With custom identifiable types struct Product: IdentifiableType, Equatable { let sku: String var name: String var price: Double var identity: String { return sku } } let productSections = [ AnimatableSectionModel(model: "Electronics", items: [ Product(sku: "E001", name: "Laptop", price: 999.99), Product(sku: "E002", name: "Mouse", price: 29.99) ]), AnimatableSectionModel(model: "Books", items: [ Product(sku: "B001", name: "Swift Guide", price: 49.99) ]) ] // Use with animated data source let dataSource = RxTableViewSectionedAnimatedDataSource>( configureCell: { ds, tv, indexPath, product in let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = product.name cell.detailTextLabel?.text = "$\(product.price)" return cell } ) ``` -------------------------------- ### Bind Simple Data to TableView with RxSwift Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md This snippet demonstrates binding a simple Observable sequence of strings to a UITableView using RxSwift's rx.items. It configures cells to display the string data. A dispose bag is required to manage the subscription. ```swift 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) ``` -------------------------------- ### Access TableViewSectionedDataSource Items and Sections via Subscript (Swift) Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Demonstrates how to directly access items and section models from a RxTableViewSectionedAnimatedDataSource using Swift's subscript syntax. This allows for easy retrieval of data based on an IndexPath or section index, which is useful within delegate methods for dynamic cell configuration or handling user interactions. ```swift import RxDataSources class TableViewController: UIViewController { var dataSource: RxTableViewSectionedAnimatedDataSource? override func viewDidLoad() { super.viewDidLoad() let dataSource = RxTableViewSectionedAnimatedDataSource( configureCell: { ds, tv, indexPath, item in let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Item \(item)" return cell } ) self.dataSource = dataSource // Bind data... } } extension TableViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // Access item directly using subscript guard let item = dataSource?[indexPath] else { return 44.0 } // Calculate dynamic height based on item return CGFloat(40 + item * 10) } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Access section guard let section = dataSource?[indexPath.section] else { return } // Access item if let item = dataSource?[indexPath] { print("Selected item \(item) from section \(section.header)") } } } ``` -------------------------------- ### RxDataSources AnimationConfiguration Struct in Swift Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Customizes animations for data source operations in RxDataSources using the `AnimationConfiguration` struct. It allows specifying different animation types for insert, reload, and delete operations. This configuration can be applied to any RxDataSources data source, such as `RxTableViewSectionedAnimatedDataSource`. ```swift import RxDataSources // Default configuration with automatic animations let defaultConfig = AnimationConfiguration() // Custom fade animations for all operations let fadeConfig = AnimationConfiguration( insertAnimation: .fade, reloadAnimation: .fade, deleteAnimation: .fade ) // Mix of different animations let mixedConfig = AnimationConfiguration( insertAnimation: .top, reloadAnimation: .automatic, deleteAnimation: .left ) // Apply to data source let dataSource = RxTableViewSectionedAnimatedDataSource( animationConfiguration: fadeConfig, configureCell: { ds, tv, indexPath, item in let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "\(item)" return cell } ) // Available animation types: // .automatic, .fade, .right, .left, .top, .bottom, .none, .middle ``` -------------------------------- ### Customize RxTableViewSectionedReloadDataSource Behavior Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Customize the behavior of the RxTableViewSectionedReloadDataSource by defining closures for various delegate methods. This includes setting titles for headers and footers, and enabling/disabling cell editing and moving. ```swift dataSource.titleForHeaderInSection = { dataSource, index in return dataSource.sectionModels[index].header } dataSource.titleForFooterInSection = { dataSource, index in // Assuming a 'footer' property exists in SectionOfCustomData // If not, this line would need adjustment or removal. // For demonstration, we'll assume it exists or is handled elsewhere. return dataSource.sectionModels[index].footer // Placeholder: Assumes footer exists } dataSource.canEditRowAtIndexPath = { dataSource, indexPath in return true } dataSource.canMoveRowAtIndexPath = { dataSource, indexPath in return true } ``` -------------------------------- ### Define Custom Section Model with SectionModelType Protocol (Swift) Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Defines a custom data structure `SectionOfCustomData` that conforms to the `SectionModelType` protocol. This allows for custom data models to be used with RxDataSources, specifying the structure for sections and their items, including header and item types. It requires the Differentiator library for its underlying diffing capabilities. ```swift import Differentiator 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 } } // Usage with data 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)) ]) ] ``` -------------------------------- ### Bind Complex Data with Animations using RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md This snippet shows how to bind a more complex data structure, SectionModel, to a UITableView using RxDataSources. It utilizes RxTableViewSectionedReloadDataSource for handling sectioned data and enables automatic reloading with animations. A dispose bag is necessary for subscription management. ```swift let dataSource = RxTableViewSectionedReloadDataSource>(configureCell: configureCell) Observable.just([SectionModel(model: "title", items: [1, 2, 3])]) .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) ``` -------------------------------- ### Bind Data to TableView using RxDataSources Source: https://github.com/rxswiftcommunity/rxdatasources/blob/main/README.md Bind an Observable sequence of custom section data to the table view using the configured RxTableViewSectionedReloadDataSource. This updates the table view with the provided data. ```swift 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) ``` -------------------------------- ### Define AnimatableSectionModelType in Swift Source: https://context7.com/rxswiftcommunity/rxdatasources/llms.txt Defines custom section models that conform to the AnimatableSectionModelType protocol, enabling automatic animations. This requires implementing the `identity` property and an initializer that takes an original and updated set of items. For custom item types, they must also conform to `IdentifiableType` and `Equatable`. ```swift import Differentiator struct MySection { var header: String var items: [Item] } extension MySection: AnimatableSectionModelType { typealias Item = Int var identity: String { return header } init(original: MySection, items: [Item]) { self = original self.items = items } } // For custom item types, they must conform to IdentifiableType and Equatable struct Task: IdentifiableType, Equatable { let id: String var title: String var completed: Bool var identity: String { return id } static func == (lhs: Task, rhs: Task) -> Bool { return lhs.id == rhs.id && lhs.title == rhs.title && lhs.completed == rhs.completed } } struct TaskSection { var header: String var items: [Task] } extension TaskSection: AnimatableSectionModelType { typealias Item = Task var identity: String { return header } init(original: TaskSection, items: [Task]) { self = original self.items = items } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.