Skip to content

Commit

Permalink
add table content
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed May 14, 2024
1 parent 5a87761 commit b84d0d0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 11 deletions.
12 changes: 8 additions & 4 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
0850AC1C2BF3A333009FDBBF /* Table-样式(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0850AC1B2BF3A333009FDBBF /* Table-样式(ap).md */; };
0850AC1E2BF3B340009FDBBF /* Table-行的选择(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0850AC1D2BF3B340009FDBBF /* Table-行的选择(ap).md */; };
0850AC202BF3B572009FDBBF /* Table-多属性排序(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0850AC1F2BF3B572009FDBBF /* Table-多属性排序(ap).md */; };
0850AC222BF41F65009FDBBF /* Table-contextMenu(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0850AC212BF41F65009FDBBF /* Table-contextMenu(ap).md */; };
08522BD627CF3218005FF059 /* Picker(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08522BD527CF3218005FF059 /* Picker(ap).md */; };
08522BDA27CF5029005FF059 /* Slider(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08522BD927CF5029005FF059 /* Slider(ap).md */; };
08522BDE27CF5133005FF059 /* Stepper(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08522BDD27CF5133005FF059 /* Stepper(ap).md */; };
Expand Down Expand Up @@ -413,6 +414,7 @@
0850AC1B2BF3A333009FDBBF /* Table-样式(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Table-样式(ap).md"; sourceTree = "<group>"; };
0850AC1D2BF3B340009FDBBF /* Table-行的选择(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Table-行的选择(ap).md"; sourceTree = "<group>"; };
0850AC1F2BF3B572009FDBBF /* Table-多属性排序(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Table-多属性排序(ap).md"; sourceTree = "<group>"; };
0850AC212BF41F65009FDBBF /* Table-contextMenu(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "Table-contextMenu(ap).md"; sourceTree = "<group>"; };
08522BD527CF3218005FF059 /* Picker(ap).md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = "Picker(ap).md"; sourceTree = "<group>"; };
08522BD927CF5029005FF059 /* Slider(ap).md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = "Slider(ap).md"; sourceTree = "<group>"; };
08522BDD27CF5133005FF059 /* Stepper(ap).md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = "Stepper(ap).md"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -926,6 +928,7 @@
0850AC1B2BF3A333009FDBBF /* Table-样式(ap).md */,
0850AC1D2BF3B340009FDBBF /* Table-行的选择(ap).md */,
0850AC1F2BF3B572009FDBBF /* Table-多属性排序(ap).md */,
0850AC212BF41F65009FDBBF /* Table-contextMenu(ap).md */,
);
path = "Table表格";
sourceTree = "<group>";
Expand Down Expand Up @@ -1524,6 +1527,7 @@
08BE636C27C8CFA7002BC6A8 /* Image(ap).md in Resources */,
08659BDD2BE9E3AA009B7C00 /* SwiftData-调试(ap).md in Resources */,
08659BDB2BE9A834009B7C00 /* SwiftData-版本迁移(ap).md in Resources */,
0850AC222BF41F65009FDBBF /* Table-contextMenu(ap).md in Resources */,
08522BE427CF5C55005FF059 /* SwiftUI颜色(ap).md in Resources */,
08448FDE279EC74200B61353 /* 比较运算符(ap).md in Resources */,
0850AC062BF2E5DA009FDBBF /* List-移动元素(ap).md in Resources */,
Expand Down Expand Up @@ -1811,7 +1815,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 9;
CURRENT_PROJECT_VERSION = 10;
DEVELOPMENT_ASSET_PATHS = "\"SwiftPamphletApp/Preview Content\"";
DEVELOPMENT_TEAM = 962Z8PV35L;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -1825,7 +1829,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 6.4.4;
MARKETING_VERSION = 6.4.5;
OTHER_LDFLAGS = (
"-Xlinker",
"-interposable",
Expand Down Expand Up @@ -1854,7 +1858,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 9;
CURRENT_PROJECT_VERSION = 10;
DEVELOPMENT_ASSET_PATHS = "\"SwiftPamphletApp/Preview Content\"";
DEVELOPMENT_TEAM = 962Z8PV35L;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -1868,7 +1872,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 6.4.4;
MARKETING_VERSION = 6.4.5;
PRODUCT_BUNDLE_IDENTIFIER = com.starming.SwiftPamphletAppByMing;
PRODUCT_NAME = "戴铭的开发小册子";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
11 changes: 6 additions & 5 deletions SwiftPamphletApp/Guide/View/GuideListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct GuideListView: View {
.foregroundStyle(i.sub == nil ? Color.secondary : .indigo)
} else if let sub = i.sub {
Image(systemName: "folder.fill")
.foregroundStyle(.gray)
.foregroundStyle(.indigo)
}
Text(listModel.searchText.isEmpty == true ? GuideListModel.simpleTitle(i.t) : i.t)
Spacer()
Expand Down Expand Up @@ -244,11 +244,12 @@ final class GuideListModel {
L(t: "LazyVGrid和LazyHGrid"),
]),
L(t: "Grid", icon: "square.grid.3x2"),
L(t: "Table表格", sub: [
L(t: "Table"),
L(t: "Table表格", icon: "tablecells.fill", sub: [
L(t: "Table", icon: "tablecells"),
L(t: "Table-样式"),
L(t: "Table-行的选择"),
L(t: "Table-多属性排序"),
L(t: "Table-行的选择", icon: "line.3.horizontal"),
L(t: "Table-多属性排序", icon: "tablecells.badge.ellipsis"),
L(t: "Table-contextMenu", icon: "filemenu.and.selection"),
]),
]),
L(t: "布局组件", sub: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

```swift
struct ContentView: View {
@State private var selection: Set<UUID> = []
var body: some View {
Table(Fruit.simpleData(), selection: $selection) {
...
}
.contextMenu(forSelectionType: Fruit.ID.self) { selection in
if selection.isEmpty {
Button("添加") {
// ...
}
} else if selection.count == 1 {
Button("收藏") {
// ...
}
} else {
Button("收藏多个") {
// ...
}
}
} primaryAction: { items in
// 双击某一行时
debugPrint(items)
}
}
...
}
```
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
# <#Title#>

你可以使用 `Table` 视图的 `sortOrder` 参数来实现多属性排序。`sortOrder` 参数接受一个绑定到一个 `SortDescriptor` 数组的变量,这个数组定义了排序的顺序和方式。

以下是一个使用 `Table` 视图实现多属性排序的例子:

```swift
struct ContentView: View {
@State private var sortOrder: [KeyPathComparator<Fruit>] = [.init(\.name, order: .reverse)]

@State var data = [
Fruit(name: "Apple", color: "Red"),
Fruit(name: "Banana", color: "Yellow"),
Fruit(name: "Cherry", color: "Red"),
Fruit(name: "Date", color: "Brown"),
Fruit(name: "Elderberry", color: "Purple")
]

var body: some View {
sortKeyPathView() // 排序状态
Table(data, sortOrder: $sortOrder) {
TableColumn("Fruit", value: \.name)
TableColumn("Color", value: \.color)
// 不含 value 参数的不支持排序
TableColumn("ColorNoOrder") {
Text("\($0.color)")
.font(.footnote)
.foregroundStyle(.mint)
}
}
.task {
data.sort(using: sortOrder)
}
.onChange(of: sortOrder) { oldValue, newValue in
data.sort(using: newValue)
}
.padding()
}

@ViewBuilder
func sortKeyPathView() -> some View {
HStack {
ForEach(sortOrder, id: \.self) { order in
Text(order.keyPath == \Fruit.name ? "名字" : "颜色")
Image(systemName: order.order == .reverse ? "chevron.down" : "chevron.up")
}
}
.padding(.top)
}
}

struct Fruit: Identifiable {
let id = UUID()
let name: String
let color: String
}
```

在这个例子中,我们首先定义了一个 `@State` 变量 `sortOrder`,它是一个 `SortDescriptor` 数组,定义了排序的顺序和方式。然后,我们将这个变量绑定到 `Table` 视图的 `sortOrder` 参数。

现在,当用户点击表头来排序一个列时,`sortOrder` 变量就会被更新。你可以使用这个变量来实现多属性排序,或者实现其他的交互功能。

Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ struct Fruit: Identifiable {

现在,当用户选择或取消选择一个元素时,selection 变量就会被更新。你可以使用这个变量来判断哪些元素被选中,或者实现其他的交互功能。

注意,为了使 Table 视图能够识别每个元素,你需要提供一个唯一的标识。在这个例子中,我们使用了 Fruit 结构体的 id 属性作为标识。

0 comments on commit b84d0d0

Please sign in to comment.