-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 列表优化。 - 添加 List 内容。 - Lazy 容器、Grid、Table 表格等内容。
- Loading branch information
Showing
25 changed files
with
956 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
SwiftPamphletApp/Resource/Guide/SwiftUI/数据集合组件/Grid(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
Grid 会将最大的一个单元格大小应用于所有单元格 | ||
|
||
代码例子: | ||
|
||
```swift | ||
struct ContentView: View { | ||
var body: some View { | ||
Grid(alignment: .center, | ||
horizontalSpacing: 30, | ||
verticalSpacing: 8) { | ||
GridRow { | ||
Text("Tropical") | ||
Text("Mango") | ||
Text("Pineapple") | ||
} | ||
GridRow(alignment: .bottom) { | ||
Text("Leafy") | ||
Text("Spinach") | ||
Text("Kale") | ||
Text("Lettuce") | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
SwiftPamphletApp/Resource/Guide/SwiftUI/数据集合组件/List列表/List-下拉刷新(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
你可以使用 `.refreshable()` 修饰符来添加下拉刷新功能。以下是一个例子: | ||
|
||
```swift | ||
struct ContentView: View { | ||
@State private var items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"] | ||
|
||
var body: some View { | ||
List { | ||
ForEach(items, id: \.self) { item in | ||
Text(item) | ||
} | ||
} | ||
.refreshable { | ||
await refresh() | ||
} | ||
} | ||
|
||
func refresh() async { | ||
// 这里是你的刷新逻辑 | ||
// 例如,你可以从网络获取新的数据,然后更新 items 数组 | ||
// 这里我们只是简单地将 items 数组反转 | ||
items.reverse() | ||
} | ||
} | ||
``` | ||
|
||
在这个例子中,我们创建了一个包含五个元素的 List,并添加了下拉刷新功能。当用户下拉 List 时,`refresh()` 方法会被调用,然后我们将 items 数组反转,从而模拟刷新操作。注意,`refresh()` 方法需要是一个异步方法,因为刷新操作通常需要一些时间来完成。 |
34 changes: 34 additions & 0 deletions
34
SwiftPamphletApp/Resource/Guide/SwiftUI/数据集合组件/List列表/List-加载更多(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
你可以通过检测列表滚动到底部来实现加载更多的功能。以下是一个简单的例子: | ||
|
||
```swift | ||
struct ContentView: View { | ||
@State private var items = Array(0..<20) | ||
|
||
var body: some View { | ||
List { | ||
ForEach(items, id: \.self) { item in | ||
Text("Item \(item)") | ||
.onAppear { | ||
if item == items.last { | ||
loadMore() | ||
} | ||
} | ||
} | ||
} | ||
.onAppear(perform: loadMore) | ||
} | ||
|
||
func loadMore() { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | ||
let newItems = Array(self.items.count..<self.items.count + 20) | ||
self.items.append(contentsOf: newItems) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
在这个例子中,我们创建了一个包含多个元素的 List。当 List 出现最后一项时,我们调用 `loadMore` 方法来加载更多的元素。在 `loadMore` 方法中,模拟在一秒后添加新的元素到 `items` 数组中。 | ||
|
||
请注意,这只是一个基本的使用示例,实际的使用方式可能会根据你的需求而变化。例如,你可能需要从网络获取新的元素,而不是像这个例子中那样直接创建新的元素。 |
123 changes: 123 additions & 0 deletions
123
SwiftPamphletApp/Resource/Guide/SwiftUI/数据集合组件/List列表/List-大纲视图(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
## List 树状结构 | ||
|
||
通过 children 参数指定子树路径。 | ||
|
||
```swift | ||
List(outlineModel, children: \.children) { i in | ||
Label(i.title, systemImage: i.iconName) | ||
} | ||
``` | ||
|
||
## DisclosureGroup 实现展开和折叠 | ||
|
||
`DisclosureGroup` 视图可以用来创建一个可以展开和折叠的内容区域。以下是一个例子: | ||
|
||
```swift | ||
struct ContentView: View { | ||
@State private var isExpanded = false | ||
|
||
var body: some View { | ||
DisclosureGroup("Options", isExpanded: $isExpanded) { | ||
Text("Option 1") | ||
Text("Option 2") | ||
Text("Option 3") | ||
} | ||
} | ||
} | ||
``` | ||
|
||
在这个例子中,我们创建了一个 `DisclosureGroup` 视图,它的标题是 "Options",并且它包含三个选项。我们使用一个 `@State` 属性 `isExpanded` 来控制 `DisclosureGroup` 视图是否展开。当用户点击标题时,`DisclosureGroup` 视图会自动展开或折叠,同时 `isExpanded` 属性的值也会相应地改变。 | ||
|
||
## OutlineGroup 创建大纲视图 | ||
|
||
可以使用 `OutlineGroup` 视图来创建一个大纲视图。以下是一个例子: | ||
|
||
```swift | ||
struct ContentView: View { | ||
var body: some View { | ||
List { | ||
OutlineGroup(sampleData, id: \.self) { item in | ||
Text(item.name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct Item: Identifiable { | ||
var id = UUID() | ||
var name: String | ||
var children: [Item]? | ||
} | ||
|
||
let sampleData: [Item] = [ | ||
Item(name: "Parent 1", children: [ | ||
Item(name: "Child 1"), | ||
Item(name: "Child 2") | ||
]), | ||
Item(name: "Parent 2", children: [ | ||
Item(name: "Child 3"), | ||
Item(name: "Child 4") | ||
]) | ||
] | ||
``` | ||
|
||
在这个例子中,我们创建了一个 `Item` 结构体,它有一个 `name` 属性和一个 `children` 属性。然后我们创建了一个 `sampleData` 数组,它包含两个父项,每个父项都有两个子项。最后我们在 `ContentView` 中使用 `OutlineGroup` 视图来显示这个数组,每个父项和子项都显示为一个文本视图。 | ||
|
||
|
||
## 结合 OutlineGroup 和 DisclosureGroup 实现自定义可折叠大纲视图 | ||
|
||
代码如下: | ||
```swift | ||
struct SPOutlineListView<D, Content>: View where D: RandomAccessCollection, D.Element: Identifiable, Content: View { | ||
private let v: SPOutlineView<D, Content> | ||
|
||
init(d: D, c: KeyPath<D.Element, D?>, content: @escaping (D.Element) -> Content) { | ||
self.v = SPOutlineView(d: d, c: c, content: content) | ||
} | ||
|
||
var body: some View { | ||
List { | ||
v | ||
} | ||
} | ||
} | ||
|
||
struct SPOutlineView<D, Content>: View where D: RandomAccessCollection, D.Element: Identifiable, Content: View { | ||
let d: D | ||
let c: KeyPath<D.Element, D?> | ||
let content: (D.Element) -> Content | ||
@State var isExpanded = true // 控制初始是否展开的状态 | ||
|
||
var body: some View { | ||
ForEach(d) { i in | ||
if let sub = i[keyPath: c] { | ||
SPDisclosureGroup(content: SPOutlineView(d: sub, c: c, content: content), label: content(i)) | ||
} else { | ||
content(i) | ||
} // end if | ||
} // end ForEach | ||
} // end body | ||
} | ||
|
||
struct SPDisclosureGroup<C, L>: View where C: View, L: View { | ||
@State var isExpanded = false | ||
var content: C | ||
var label: L | ||
var body: some View { | ||
DisclosureGroup(isExpanded: $isExpanded) { | ||
content | ||
} label: { | ||
Button { | ||
withAnimation { | ||
isExpanded.toggle() | ||
} | ||
} label: { | ||
label | ||
} | ||
.buttonStyle(.plain) | ||
} | ||
|
||
} | ||
} | ||
``` |
21 changes: 21 additions & 0 deletions
21
SwiftPamphletApp/Resource/Guide/SwiftUI/数据集合组件/List列表/List-完全可点击的行(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
使用 `.contentShape(Rectangle())` 可以使整个区域都可点击 | ||
|
||
```swift | ||
struct ContentView: View { | ||
var body: some View { | ||
List { | ||
ForEach(1..<50) { num in | ||
HStack { | ||
Text("\(num)") | ||
Spacer() | ||
} | ||
.contentShape(Rectangle()) | ||
.onTapGesture { | ||
print("Clicked \(num)") | ||
} | ||
} | ||
} // end list | ||
} | ||
} | ||
``` |
Oops, something went wrong.