Travis CI

表格视图和集合视图数据源

特性

为什么

编写表格视图和集合视图数据源非常繁琐。即使是最简单的情况,也需要实现大量的代理方法。

RxSwift 通过简单的数据绑定机制帮助减轻了一些负担

  1. 将您的数据转换为 Observable 序列
  2. 使用以下方法之一将数据绑定到 tableView/collectionView
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)

这对于简单的数据集效果很好,但不能处理需要绑定具有多个 section 的复杂数据集,或者需要在添加/修改/删除 item 时执行动画的情况。

这些正是 RxDataSources 旨在解决的用例。

使用 RxDataSources,编写以下代码非常容易

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

如何

给定以下自定义数据结构

struct CustomData {
  var anInt: Int
  var aString: String
  var aCGPoint: CGPoint
}
  1. 首先定义您的 section,使用符合 SectionModelType 协议的结构体
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. 创建一个 dataSource 对象,并将您的 SectionOfCustomData 类型传递给它
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. 根据需要自定义 dataSource 上的闭包
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. 将实际数据定义为 CustomData 对象的 Observable 序列,并将其绑定到 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)

动画数据源

RxDataSources 提供了两种特殊的数据源类型,可以自动处理绑定数据源中的动画更改:RxTableViewSectionedAnimatedDataSourceRxCollectionViewSectionedAnimatedDataSource

要使用其中一个动画数据源,您必须在上面概述的步骤之外再执行一些额外的步骤

要求

Xcode 10.2

Swift 5.0

对于 Swift 4.x 版本,请使用版本 3.0.0 ... 3.1.0 对于 Swift 3.x 版本,请使用版本 1.0 ... 2.0.2 对于 Swift 2.3 版本,请使用版本 0.1 ... 0.9

安装

我们将尝试保持 API 尽可能稳定,但可能会发生破坏性的 API 更改。

CocoaPods

Podfile

pod 'RxDataSources', '~> 5.0'

Carthage

Cartfile

github "RxSwiftCommunity/RxDataSources" ~> 5.0

Swift Package Manager

创建一个 Package.swift 文件。

import PackageDescription

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

如果您使用的是 Xcode 11 或更高版本,请转到File / Swift Packages / Add Package Dependency...,然后输入包存储库 URL https://github.com/RxSwiftCommunity/RxDataSources.git,然后按照说明进行操作。