Combine Data Sources

CombineDataSources 提供了自定义的 Combine 订阅器,这些订阅器充当表格和集合视图控制器,并将元素集合流绑定到带有单元格的表格或集合部分。

⚠️ ⚠️ ⚠️ 注意 🚨🚨🚨: 此软件包目前仍在开发中。

目录

  1. 用法

1.1 绑定元素的纯列表

1.2 绑定 Section 模型列表

1.2 自定义列表控制器

1.3 分批加载列表

  1. 安装

2.1 Swift Package Manager

2.2 Cocoapods

  1. 许可证

  2. 鸣谢


用法

演示应用 📱

该仓库在 Example 子文件夹中包含一个演示应用,演示了在实践中使用 CombineDataSources 的不同方法。

绑定元素的纯列表

var data = PassthroughSubject<[Person], Never>()

data
  .bind(subscriber: tableView.rowsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
    cell.nameLabel.text = model.name
  }))
  .store(in: &subscriptions)

Plain list updates with CombineDataSources

分别为集合视图

data
  .bind(subscriber: collectionView.itemsSubscriber(cellIdentifier: "Cell", cellType: PersonCollectionCell.self, cellConfig: { cell, indexPath, model in
    cell.nameLabel.text = model.name
    cell.imageURL = URL(string: "https://api.adorable.io/avatars/100/\(model.name)")!
  }))
  .store(in: &subscriptions)

Plain list updates for a collection view

绑定 Section 模型列表

var data = PassthroughSubject<[Section<Person>], Never>()

data
  .bind(subscriber: tableView.sectionsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
    cell.nameLabel.text = model.name
  }))
  .store(in: &subscriptions)

Sectioned list updates with CombineDataSources

自定义表格控制器

var data = PassthroughSubject<[[Person]], Never>()

let controller = TableViewItemsController<[[Person]]>(cellIdentifier: "Cell", cellType: PersonCell.self) { cell, indexPath, person in
  cell.nameLabel.text = person.name
}
controller.animated = false

// More custom controller configuration ...

data
  .bind(subscriber: tableView.sectionsSubscriber(controller))
  .store(in: &subscriptions)

分批加载列表

列表视图的常见模式是以“批次”或“页面”加载非常长的元素列表。(区别在于页面意味着有序的,等长的批次。)

CombineDataSources 包含一个数据源,允许您轻松实现名为 BatchesDataSource 的分批列表模式,以及一个表格视图控制器 TableViewBatchesController,它通过上述数据源包装了分批加载项目并管理您的 UI。

如果您想实现自己的自定义逻辑,可以直接使用数据源类型

let input = BatchesInput(
  reload: resetSubject.eraseToAnyPublisher(),
  loadNext: loadNextSubject.eraseToAnyPublisher()
)

let dataSource = BatchesDataSource<String>(
  items: ["Initial Element"],
  input: input,
  initialToken: nil,
  loadItemsWithToken: { token in
    return MockAPI.requestBatchCustomToken(token)
  })

dataSource 通过两个输入控制

如果您想使用提供的控制器,代码也相当简单。您可以像这样使用标准表格视图项目控制器和 TableViewBatchesController

let itemsController = TableViewItemsController<[[String]]>(cellIdentifier: "Cell", cellType: UITableViewCell.self, cellConfig: { cell, indexPath, text in
  cell.textLabel!.text = "\(indexPath.row+1). \(text)"
})

let tableController = TableViewBatchesController<String>(
  tableView: tableView,
  itemsController: itemsController,
  initialToken: nil,
  loadItemsWithToken: { nextToken in
    MockAPI.requestBatch(token: nextToken)
  }
)

tableController 将设置表格视图数据源,获取项目,并使用适当的动画显示单元格。

待办事项

安装

Swift Package Manager

将以下依赖项添加到您的 Package.swift 文件中

.package(url: "https://github.com/combineopensource/CombineDataSources, from: "0.2")

Cocoapods

将以下依赖项添加到您的 Podfile

pod 'CombineDataSources'

许可证

CombineOpenSource 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。

Combine 开源

Combine Slack channel

CombineOpenSource Slack 频道: https://combineopensource.slack.com

在此注册

鸣谢

由 Marin Todorov 为 CombineOpenSource 创建。

📚 您可以通过查看我们的 Combine 书籍来支持我: combinebook.com

灵感来自 RxDataSourcesRxRealmDataSources