ASCollectionView
UICollectionView & UITableView 的 SwiftUI 实现。以下是其一些有用功能
- 支持 预加载 和 onAppear/onDisappear。
- 支持 单元格选择,自动支持 SwiftUI 编辑模式。
- 支持单元格的 自动大小调整。
- 支持新的 UICollectionViewCompositionalLayout 以及 任何其他 UICollectionViewLayout。
- 支持为 ASTableView 移除分隔符。
- 直接支持将 FetchedResults 作为数据源。
欢迎 pull requests 和建议 :)
目录
演示应用截图
使用本工具包需要启动,请点击下面的链接:
ASCollectionView是一个swift工具包。
- 您可以使用Xcode的“File”菜单中的新“Swift Packages”选项将其导入到应用项目中。
- 当被询问时,请使用以下GitHub仓库的URL:https://github.com/apptekstudios/ASCollectionView
使用方法请点击以下链接:
import ASCollectionView
import SwiftUI
struct SingleSectionExampleView: View {
@State var dataExample = (0 ..< 30).map { $0 }
var body: some View
{
ASCollectionView(data: dataExample, dataID: \.self) { item, _ in
Color.blue
.overlay(Text("\(item)"))
}
.layout {
.grid(layoutMode: .adaptive(withMinItemSize: 100),
itemSpacing: 5,
lineSpacing: 5,
itemSize: .absolute(50))
}
}
}
以下是如何包含包含两个分区的集合视图的示例(每个都有其自己的数据源)。对于使用自定义组合布局的扩展示例,请参阅此处。或者,要了解更多示例,请下载此存储库中包含的演示项目。
import SwiftUI
import ASCollectionView
struct ExampleView: View {
@State var dataExampleA = (0 ..< 21).map { $0 }
@State var dataExampleB = (0 ..< 15).map { "ITEM \($0)" }
var body: some View
{
ASCollectionView
{
ASCollectionViewSection(
id: 0,
data: dataExampleA,
dataID: \.self)
{ item, _ in
Color.blue
.overlay(
Text("\(item)")
)
}
ASCollectionViewSection(
id: 1,
data: dataExampleB,
dataID: \.self)
{ item, _ in
Color.green
.overlay(
Text("Complex layout - \(item)")
)
}
.sectionHeader
{
HStack
{
Text("Section header")
.padding()
Spacer()
}
.background(Color.yellow)
}
.sectionFooter
{
Text("This is a section footer!")
.padding()
}
}
.layout { sectionID in
switch sectionID {
case 0:
// Here we use one of the provided convenience layouts
return .grid(layoutMode: .adaptive(withMinItemSize: 100),
itemSpacing: 5,
lineSpacing: 5,
itemSize: .absolute(50))
default:
return ASCollectionLayoutSection { environment in
// ...
// You could return any custom NSCollectionLayoutSection here. For an example see this file: /readmeAssets/SampleUsage.swift
// ...
}
}
}
}
}
辅助视图
ASCollectionView支持辅助视图。要添加辅助视图,请使用您的ASCollectionViewSection
上的sectionHeader
、sectionFooter
或sectionSupplementary
修饰符。
sectionHeader
和sectionFooter
分别为UICollectionView.elementKindSectionHeader
和UICollectionView.elementKindSectionFooter
设置辅助视图。sectionSupplementary
让您可以指定任何辅助视图类型。
ASCollectionViewSection(...) { ... }
.sectionHeader
{
Text("Section header")
.background(Color.yellow)
}
.sectionFooter
{
Text("Section footer")
.background(Color.blue)
}
.sectionSupplementary(ofKind: "someOtherSupplementaryKindRequestedByYourLayout") {
Text("Section supplementary")
.background(Color.green)
}
装饰视图
UICollectionViewLayout 可以布局与数据无关的装饰视图(例如,区域背景)。这些不能配置,因此您必须提供一个可以使用.init()初始化的 View 结构体。
- 为了强制这一要求,您的视图必须遵守
Decoration
协议。这个协议的要求只有一个,那就是一个不带参数的初始化器。 - 您必须将视图类型注册到布局中。
- 查看示例应用程序的“提醒”屏幕,以获取有效示例。
声明遵循 Decoration
的 Swift 视图
struct GroupBackground: View, Decoration
{
let cornerRadius: CGFloat = 12
var body: some View
{
RoundedRectangle(cornerRadius: cornerRadius)
.fill(Color(.secondarySystemGroupedBackground))
}
}
将装饰类型注册到布局中(ASCollectionLayout)
var layout: ASCollectionLayout<Section>
{
ASCollectionLayout<Section>
{
// ... Here is an example of including a decoration in a compositional layout.
let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(elementKind: "groupBackground")
sectionBackgroundDecoration.contentInsets = section.contentInsets
section.decorationItems = [sectionBackgroundDecoration]
// ...
}
.decorationView(GroupBackground.self, forDecorationViewOfKind: "groupBackground") //REGISTER the decoration view type
布局
- 支持新的 UICollectionViewCompositionalLayout。
- 您可以在每个区域的基础上定义布局,包括如果需要,使用 switch 语句。
- 工作进行中:有一些有用的方法可以轻松地定义基于列表和网格的布局(包括正交网格)。
定义所有区域的布局
ASCollectionView(...) { ... }
.layout {
ASCollectionLayoutSection { layoutEnvironment in
//Construct and return a NSCollectionLayoutSection here
}
}
按区域定义布局
ASCollectionView(...) { ... }
.layout { sectionID in
switch sectionID {
case .userSection:
return ASCollectionLayoutSection { layoutEnvironment in
//Construct and return a NSCollectionLayoutSection here
}
}
case .postSection:
return ASCollectionLayoutSection { layoutEnvironment in
//Construct and return a NSCollectionLayoutSection here
}
}
}
使用自定义 UICollectionViewLayout
ASCollectionView(...) { ... }
.layout {
let someCustomLayout = CustomUICollectionViewLayout()
someCustomLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
return someCustomLayout
}
其他提示
- 您可以使用枚举作为您的 SectionID(而不仅仅是 Int),这可以让您轻松确定每个区域的布局。
- 请参阅示例项目以获得更深入的使用示例。
- 请注意,您只应在集合视图中使用 @State 以维护瞬态可见状态。您想长期存储的任何内容都应存储在您的模型中。
待办事项
请参阅公开问题以获取功能建议(以及已知问题)的列表。
许可证
基于MIT协议分发。更多信息请参阅 LICENSE
文件。