CellsReloadable 提供了一种简化的方法来管理 iOS 中的表格视图、集合视图和堆栈视图。通过消除对 UITableViewCell
或 UICollectionViewCell
进行子类的需求,该库简化了视图重载的过程。 使用 CellsReloadable,开发者可以直接使用 UIView
或 SwiftUI View
实例,从而实现更模块化和高效的 UI 实现。
UIView
单元格重载reloader.reload(with: myData) { _ in
MyCustomUIView()
} render: { view, data in
view.render(with: data)
}
RenderableView
重载reloader.reload(with: myData) {
MyCustomRenderableView()
}
reloader.reload {
MyCustomUIView.Cell(with: props)
if someCondition {
MyCustomSwiftUIView()
}
MyCustomStaticUIView.Cell()
}
reloader.reload(with: myData) { data in
MyCustomSwiftUIView(data)
}
reloader.reload(with: myData) { item in
switch item.type {
case let .swiftUICell(props):
SomeSwiftUIView(props)
.asViewCell(id: props.id)
case let .uiKitUICell(props):
MyCustomUIView.Cell(with: props)
.height(72)
}
}
reloader.reload {
for i in 0..<10 {
UILabel.Cell(id: i) {
$0.text = "Item \(i)"
}
.height(50)
}
}
reloader.reload {
CellsSection(data: myData) {
MyCustomRenderableView()
}
.cellsWith(\.height, 72)
CellsSection(data: myData) {
SomeSwiftUIView($0)
}
.header {
Text("Header").padding()
}
}
这个库为 UITableView
、UIStackView
和 UICollectionView
提供了可重载的功能。
它引入了几种类型
ViewCell
: 一个可识别的结构体,用于表示视图中的一个单元格。
UIView.Cell
类型别名: 一个辅助类型别名,用于内联创建 ViewCell
。
UITableViewReloader
: 一个类,用于在使用 UITableView
时绕过传统的数据源方法。
UICollectionViewReloader
: 一个类,用于在使用 UICollectionView
时绕过传统的数据源方法。
ViewCellsReloadable
: 一个协议,用于标记一个视图具有可重载的单元格。由 UITableViewReloader
、UIStackView
和 UICollectionViewReloader
实现。
ViewCellConvertible
: 一个可识别的结构体,用于表示视图中的一个单元格。
ViewCellsBuilder
: 一个枚举,用于辅助构建单元格。
CellsSectionsReloadable
: 一个协议,用于具有可重载 section 的视图。由 UITableViewReloader
、UICollectionViewReloader
和 UIStackView
实现。
CellsSection
: 一个可识别的结构体,用于表示一个单元格的 section。
CellsSectionConvertable
: 一个协议,用于将自定义结构体转换为 CellsSection
结构。
CellsSectionsBuilder
: 一个枚举,用于辅助构建单元格的 section。
RenderableView
: 一个便捷的协议,用于表示能够动态渲染内容的视图。
这些元素共同为你的视图提供强大的重载能力。 这些能力通过几个 reload
函数提供,这些函数接收一个闭包来生成单元格,以及用于定义单元格创建、重载、大小调整和识别的可选参数。
以下是一些提供的功能
reload { }
: 使用一个闭包来重载可重载视图中的单元格,该闭包创建一个 ViewCell
、UIView
、View
或 CellsSection
数组。reload(with data:id:create:render:completion:)
: 使用数据、一个生成唯一标识符的函数、用于创建和重载单元格的闭包以及一个用于调整单元格大小的闭包来重载可重载视图中的单元格。 completion
和 id
是可选的。 只有当你想对重载进行动画处理时才需要 id
参数。 如果它是 SwiftUI View
或 RenderableView
视图,并且 data
是其 Props
的集合,则可以省略 render
参数。这些 reload
函数使用各种参数来控制视图中单元格的重载。 通过使用这些函数,你可以轻松管理视图中的单元格,并确保它们始终是最新的。
建议为 section 和单元格指定一个 id
,或者为正确的动画使用 Identifiable
项目。 对于 SwiftUI 视图,有一个 asViewCell(id:)
方法。
UITableViewReloader
是一个类,它消除了使用传统数据源的需要。 它允许你直接处理数据和应该显示的单元格。 借助此功能,你无需对 UITableViewCell
进行子类化。 相反,你可以直接使用 UIView
实例,使你的代码库更简单、更干净。
这是一个例子
lazy var tableSource = UITableViewReloader(tableView)
let tableView = UITableView()
tableSource.reload(with: myData) { _ in
MyCustomView()
} render: { view, data in
view.render(with: data)
}
与 UITableViewReloader
类似,UICollectionViewReloader
是一个扩展,允许你在使用 UICollectionView
时绕过传统的数据源和委托方法。 你不必对 UICollectionViewCell
进行子类化。 你可以直接使用 UIView
实例,并将它们简单地绑定到你要显示的数据。
这是一个例子
lazy var collectionSource = UICollectionViewReloader(collectionView)
let collectionView = UICollectionView()
collectionSource.reload(with: myData) { _ in
MyCustomView()
} render: { view, data in
view.configure(with: data)
}
ViewCell
是一个结构体,它充当视图中单元格的表示。 ViewCell
的主要目的是为传统单元格提供一个抽象层,允许你直接使用 UIView
实例,而不是处理对特定单元格类进行子类化的开销。
一个动态容器,允许你将自定义存储的属性添加到该结构体中。 这对于添加额外的功能(如自定义布局、行为甚至与单元格关联的元数据)可能是有益的。
扩展 ViewCell.Values
的示例
extension ViewCell.Values {
var style: CellStyle {
self[\.style] ?? .default
}
}
设置样式的示例
ViewCell {
SomeCell()
}
.with(\.style, .warning)
CellsSection
是一个结构,旨在表示支持分段布局的视图(如 UITableView
和 UICollectionView
)中的单元格 section。
与 ViewCell.Values
类似,这个动态容器允许你将自定义存储的属性添加到该结构体中。 当你想为给定的 section 引入诸如 header、footer 或特定布局之类的属性时,这可能特别有用。
RenderableView
协议用于表示能够动态渲染内容的视图。
符合此协议的视图应实现一种基于提供的数据渲染其内容的方法。
自定义结构体可以采用 ViewCellConvertible
协议来提供一种将它们转换为 ViewCell
结构的机制。 这有助于将自定义结构体直接集成到可重载的视图中,而无需 ViewCell
包装。 例子
extension MyView.Props: ViewCellConvertible {
public var asViewCell: ViewCell {
ViewCell(props: self) {
MyView()
}
.height(70)
.willReuse(MyView.self) {
$0.prepareForReuse()
}
}
}
reloader.reload {
MyView.Props(.first)
MyView.Props(.second)
}
reloader.reload(with: collectionOfProps)
与 ViewCellConvertible
协议类似,CellsSectionConvertible
协议允许将自定义结构体表示直接转换为 CellsSection
结构。 当你想要将预定义的 section 模型无缝集成到可重载的视图中时,这非常方便。
创建一个 Package.swift
文件。
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/CellsReloadable.git", from: "1.1.1")
],
targets: [
.target(name: "SomeProject", dependencies: ["CellsReloadable"])
]
)
$ swift build
dankinsoid, voidilov@gmail.com
CellsReloadable 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。