CellsReloadable

CellsReloadable 提供了一种简化的方法来管理 iOS 中的表格视图、集合视图和堆栈视图。通过消除对 UITableViewCellUICollectionViewCell 进行子类的需求,该库简化了视图重载的过程。 使用 CellsReloadable,开发者可以直接使用 UIView 或 SwiftUI View 实例,从而实现更模块化和高效的 UI 实现。

一些代码示例

reloader.reload(with: myData) { _ in
    MyCustomUIView()
} render: { view, data in
    view.render(with: data)
}
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()
  }
}

描述

这个库为 UITableViewUIStackViewUICollectionView 提供了可重载的功能。

它引入了几种类型

这些元素共同为你的视图提供强大的重载能力。 这些能力通过几个 reload 函数提供,这些函数接收一个闭包来生成单元格,以及用于定义单元格创建、重载、大小调整和识别的可选参数。

以下是一些提供的功能

这些 reload 函数使用各种参数来控制视图中单元格的重载。 通过使用这些函数,你可以轻松管理视图中的单元格,并确保它们始终是最新的。

建议为 section 和单元格指定一个 id,或者为正确的动画使用 Identifiable 项目。 对于 SwiftUI 视图,有一个 asViewCell(id:) 方法。

UITableViewReloader

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)
}

UICollectionViewReloader

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 是一个结构体,它充当视图中单元格的表示。 ViewCell 的主要目的是为传统单元格提供一个抽象层,允许你直接使用 UIView 实例,而不是处理对特定单元格类进行子类化的开销。

ViewCell.Values

一个动态容器,允许你将自定义存储的属性添加到该结构体中。 这对于添加额外的功能(如自定义布局、行为甚至与单元格关联的元数据)可能是有益的。
扩展 ViewCell.Values 的示例

extension ViewCell.Values {
  
  var style: CellStyle {
    self[\.style] ?? .default
  }
}

设置样式的示例

ViewCell {
  SomeCell()
}
.with(\.style, .warning)

CellsSection

CellsSection 是一个结构,旨在表示支持分段布局的视图(如 UITableViewUICollectionView)中的单元格 section。

CellsSection.Values

ViewCell.Values 类似,这个动态容器允许你将自定义存储的属性添加到该结构体中。 当你想为给定的 section 引入诸如 header、footer 或特定布局之类的属性时,这可能特别有用。

RenderableView

RenderableView 协议用于表示能够动态渲染内容的视图。
符合此协议的视图应实现一种基于提供的数据渲染其内容的方法。

ViewCellConvertible

自定义结构体可以采用 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)

CellsSectionConvertible

ViewCellConvertible 协议类似,CellsSectionConvertible 协议允许将自定义结构体表示直接转换为 CellsSection 结构。 当你想要将预定义的 section 模型无缝集成到可重载的视图中时,这非常方便。

安装

  1. Swift Package Manager

创建一个 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 文件。