-
Notifications
You must be signed in to change notification settings - Fork 6
/
swiftui-expandable-list.html
1097 lines (634 loc) · 48 KB
/
swiftui-expandable-list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="zh-tw" >
<head>
<meta charset=utf-8"UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=11; IE=10; IE=9; IE=8; IE=7; IE=EDGE" />
<title>如何建立展开式列表视图和大纲视图 | 精通 SwiftUI - iOS 16 版</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.0.1">
<meta name="author" content="Simon Ng">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="./swiftui-gridlayout.html" />
<link rel="prev" href="./swiftui-carousel.html" />
</head>
<body>
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-image-captions/image-captions.css">
<link rel="stylesheet" href="././styles/website.css">
<div class="book" data-level="28" data-basepath="." data-revision="Thu Dec 01 2022 17:36:55 GMT+0800 (HKT)">
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="输入并搜寻" class="form-control" />
</div>
<ul class="summary">
<li>
<a href="https://www.appcoda.com.tw" target="blank" class="custom-link">Made by AppCoda</a>
</li>
<li>
<a href="https://www.appcoda.com.tw/contact" target="blank" class="custom-link">Contact us / Support</a>
</li>
<li>
<a href="http://twitter.com/share?text=Mastering%20SwiftUI%20via%20@appcodamobile&url=https://www.appcoda.com.tw/swiftui/&hashtags=swiftlang" target="blank" class="custom-link">Tweet this book</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
序言
</a>
</li>
<li class="chapter " data-level="1" data-path="swiftui-basics.html">
<a href="./swiftui-basics.html">
<i class="fa fa-check"></i>
<b>1.</b>
SwiftUI 介绍
</a>
</li>
<li class="chapter " data-level="2" data-path="swiftui-text.html">
<a href="./swiftui-text.html">
<i class="fa fa-check"></i>
<b>2.</b>
SwiftUI 入门 - 文字的处理
</a>
</li>
<li class="chapter " data-level="3" data-path="swiftui-images.html">
<a href="./swiftui-images.html">
<i class="fa fa-check"></i>
<b>3.</b>
图片的处理
</a>
</li>
<li class="chapter " data-level="4" data-path="swiftui-stacks.html">
<a href="./swiftui-stacks.html">
<i class="fa fa-check"></i>
<b>4.</b>
以堆叠布局使用者介面
</a>
</li>
<li class="chapter " data-level="5" data-path="swiftui-scrollview.html">
<a href="./swiftui-scrollview.html">
<i class="fa fa-check"></i>
<b>5.</b>
ScrollView 与 Carousel UI 的建立
</a>
</li>
<li class="chapter " data-level="6" data-path="swiftui-buttons.html">
<a href="./swiftui-buttons.html">
<i class="fa fa-check"></i>
<b>6.</b>
SwiftUI 按钮与渐层
</a>
</li>
<li class="chapter " data-level="7" data-path="swiftui-state.html">
<a href="./swiftui-state.html">
<i class="fa fa-check"></i>
<b>7.</b>
状态(State)与绑定(Binding)
</a>
</li>
<li class="chapter " data-level="8" data-path="swiftui-path-shape.html">
<a href="./swiftui-path-shape.html">
<i class="fa fa-check"></i>
<b>8.</b>
实现路径(Path)与形状(Shape)来画线与圆饼图
</a>
</li>
<li class="chapter " data-level="9" data-path="swiftui-animation.html">
<a href="./swiftui-animation.html">
<i class="fa fa-check"></i>
<b>9.</b>
基础动画与转场
</a>
</li>
<li class="chapter " data-level="10" data-path="swiftui-list.html">
<a href="./swiftui-list.html">
<i class="fa fa-check"></i>
<b>10.</b>
动态列表、 ForEach 与 Identifiable 的使用方法
</a>
</li>
<li class="chapter " data-level="11" data-path="swiftui-navigation.html">
<a href="./swiftui-navigation.html">
<i class="fa fa-check"></i>
<b>11.</b>
导航UI与导航列客制化运用
</a>
</li>
<li class="chapter " data-level="12" data-path="swiftui-modal.html">
<a href="./swiftui-modal.html">
<i class="fa fa-check"></i>
<b>12.</b>
强制回应视图、浮动按钮与提示的实现
</a>
</li>
<li class="chapter " data-level="13" data-path="swiftui-form.html">
<a href="./swiftui-form.html">
<i class="fa fa-check"></i>
<b>13.</b>
以选取器(Picker)、开关(Toggle)与步进器(Stepper)来建立一个表单
</a>
</li>
<li class="chapter " data-level="14" data-path="swiftui-form-data.html">
<a href="./swiftui-form-data.html">
<i class="fa fa-check"></i>
<b>14.</b>
使用 Combine 与 Environment 物件进行数据分享
</a>
</li>
<li class="chapter " data-level="15" data-path="swiftui-form-registration.html">
<a href="./swiftui-form-registration.html">
<i class="fa fa-check"></i>
<b>15.</b>
以 Combine 与 视图模型建立一个注册表单
</a>
</li>
<li class="chapter " data-level="16" data-path="swiftui-actionsheet-context.html">
<a href="./swiftui-actionsheet-context.html">
<i class="fa fa-check"></i>
<b>16.</b>
滑动删除、内容菜单与动作列表
</a>
</li>
<li class="chapter " data-level="17" data-path="swiftui-gestures.html">
<a href="./swiftui-gestures.html">
<i class="fa fa-check"></i>
<b>17.</b>
认识手势(Gestures)
</a>
</li>
<li class="chapter " data-level="18" data-path="swiftui-bottom-sheet.html">
<a href="./swiftui-bottom-sheet.html">
<i class="fa fa-check"></i>
<b>18.</b>
以SwiftUI 手势与 GeometryReader 建立一个底部展开式页面
</a>
</li>
<li class="chapter " data-level="19" data-path="swiftui-trip-tinder.html">
<a href="./swiftui-trip-tinder.html">
<i class="fa fa-check"></i>
<b>19.</b>
使用手势与动画建立 Tinder 风格的 UI
</a>
</li>
<li class="chapter " data-level="20" data-path="swiftui-advanced-animations.html">
<a href="./swiftui-advanced-animations.html">
<i class="fa fa-check"></i>
<b>20.</b>
建立像 Apple Wallet App 的动画和转场效果
</a>
</li>
<li class="chapter " data-level="21" data-path="swiftui-json.html">
<a href="./swiftui-json.html">
<i class="fa fa-check"></i>
<b>21.</b>
JSON、滑杆的运用与数据过滤
</a>
</li>
<li class="chapter " data-level="22" data-path="swiftui-core-data.html">
<a href="./swiftui-core-data.html">
<i class="fa fa-check"></i>
<b>22.</b>
如何使用 Core Data 建立 ToDo App
</a>
</li>
<li class="chapter " data-level="23" data-path="swiftui-uikit.html">
<a href="./swiftui-uikit.html">
<i class="fa fa-check"></i>
<b>23.</b>
利用 UIViewRepresentable 整合UIKit 组件
</a>
</li>
<li class="chapter " data-level="24" data-path="swiftui-searchbar.html">
<a href="./swiftui-searchbar.html">
<i class="fa fa-check"></i>
<b>24.</b>
建立搜寻栏视图并使用自订绑定(Custom Binding)
</a>
</li>
<li class="chapter " data-level="25" data-path="swiftui-real-world-app.html">
<a href="./swiftui-real-world-app.html">
<i class="fa fa-check"></i>
<b>25.</b>
把所学应用出来!构建个人理财App
</a>
</li>
<li class="chapter " data-level="26" data-path="swiftui-appstoreanimation.html">
<a href="./swiftui-appstoreanimation.html">
<i class="fa fa-check"></i>
<b>26.</b>
创建类似App Store使用的动画视图转换
</a>
</li>
<li class="chapter " data-level="27" data-path="swiftui-carousel.html">
<a href="./swiftui-carousel.html">
<i class="fa fa-check"></i>
<b>27.</b>
如何建立图像轮播(Image Carousel)
</a>
</li>
<li class="chapter active" data-level="28" data-path="swiftui-expandable-list.html">
<a href="./swiftui-expandable-list.html">
<i class="fa fa-check"></i>
<b>28.</b>
如何建立展开式列表视图和大纲视图
</a>
</li>
<li class="chapter " data-level="29" data-path="swiftui-gridlayout.html">
<a href="./swiftui-gridlayout.html">
<i class="fa fa-check"></i>
<b>29.</b>
使用 LazyVGrid 和 LazyHGrid 构建集合视图
</a>
</li>
<li class="chapter " data-level="30" data-path="swiftui-progress-ring.html">
<a href="./swiftui-progress-ring.html">
<i class="fa fa-check"></i>
<b>30.</b>
使用 Shape 和 Animatable 开发带动画的环形进度条
</a>
</li>
<li class="chapter " data-level="31" data-path="swiftui-library.html">
<a href="./swiftui-library.html">
<i class="fa fa-check"></i>
<b>31.</b>
如何使用 AnimatableModifier 和 LibraryContentProvider
</a>
</li>
<li class="chapter " data-level="32" data-path="swiftui-texteditor.html">
<a href="./swiftui-texteditor.html">
<i class="fa fa-check"></i>
<b>32.</b>
使用 TextEditor 支持多行文字输入
</a>
</li>
<li class="chapter " data-level="33" data-path="swiftui-matchedgeometry.html">
<a href="./swiftui-matchedgeometry.html">
<i class="fa fa-check"></i>
<b>33.</b>
使用 matchedGeometryEffect为 App 建立绚丽的视图动画
</a>
</li>
<li class="chapter " data-level="34" data-path="swiftui-grid-animation.html">
<a href="./swiftui-grid-animation.html">
<i class="fa fa-check"></i>
<b>34.</b>
ScrollViewReader 和网格动画
</a>
</li>
<li class="chapter " data-level="35" data-path="swiftui-tabview.html">
<a href="./swiftui-tabview.html">
<i class="fa fa-check"></i>
<b>35.</b>
标签视图的运用与自订标签列
</a>
</li>
<li class="chapter " data-level="36" data-path="swiftui-asyncimage.html">
<a href="./swiftui-asyncimage.html">
<i class="fa fa-check"></i>
<b>36.</b>
利用 AsyncImage 非同步加载和显示图像
</a>
</li>
<li class="chapter " data-level="37" data-path="swiftui-searchable.html">
<a href="./swiftui-searchable.html">
<i class="fa fa-check"></i>
<b>37.</b>
利用 Searchable 建立搜寻栏
</a>
</li>
<li class="chapter " data-level="38" data-path="swiftui-charts.html">
<a href="./swiftui-charts.html">
<i class="fa fa-check"></i>
<b>38.</b>
利用 Charts 框架建立图表
</a>
</li>
<li class="chapter " data-level="39" data-path="swiftui-live-text.html">
<a href="./swiftui-live-text.html">
<i class="fa fa-check"></i>
<b>39.</b>
利用 Live Text API 从图片中撷取文本
</a>
</li>
<li class="chapter " data-level="40" data-path="swiftui-sharelink.html">
<a href="./swiftui-sharelink.html">
<i class="fa fa-check"></i>
<b>40.</b>
通过 ShareLink 来分享文本和图像等数据
</a>
</li>
<li class="chapter " data-level="41" data-path="swiftui-imagerenderer.html">
<a href="./swiftui-imagerenderer.html">
<i class="fa fa-check"></i>
<b>41.</b>
利用 ImageRenderer API 轻松把 SwiftUI 视图转换为图像
</a>
</li>
<li class="chapter " data-level="42" data-path="swiftui-pdf-doc.html">
<a href="./swiftui-pdf-doc.html">
<i class="fa fa-check"></i>
<b>42.</b>
如何把 SwiftUI 视图转换为 PDF 文件
</a>
</li>
<li class="chapter " data-level="43" data-path="swiftui-gauge.html">
<a href="./swiftui-gauge.html">
<i class="fa fa-check"></i>
<b>43.</b>
使用 Gauge 视图显示进度并创建速度计
</a>
</li>
<li class="chapter " data-level="44" data-path="swiftui-grid.html">
<a href="./swiftui-grid.html">
<i class="fa fa-check"></i>
<b>44.</b>
使用Grid API 创建网格布局
</a>
</li>
<li class="chapter " data-level="45" data-path="swiftui-anylayout.html">
<a href="./swiftui-anylayout.html">
<i class="fa fa-check"></i>
<b>45.</b>
利用 AnyLayout 切换 UI 布局
</a>
</li>
<li class="chapter " data-level="46" data-path="swiftui-navigationstack.html">
<a href="./swiftui-navigationstack.html">
<i class="fa fa-check"></i>
<b>46.</b>
使用新的 NavigationStack 视图构建数据导向的导航
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 释出
</a>
</li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="目录"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="搜寻"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="字型设定"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">衬线体</button>
<button type="button" data-font="1" class="button">无衬线体</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">白色</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">棕褐色</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">夜间</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="分享"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">
分享到 Twitter
</button>
<button type="button" data-sharing="google-plus" class="button">
分享到 Google
</button>
<button type="button" data-sharing="facebook" class="button">
分享到 Facebook
</button>
<button type="button" data-sharing="weibo" class="button">
分享到 Weibo
</button>
<button type="button" data-sharing="instapaper" class="button">
分享到 Instapaper
</button>
</div>
</div>
</div>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >精通 SwiftUI - iOS 16 版</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="%E7%AC%AC-28-%E7%AB%A0-br-%E5%A6%82%E4%BD%95%E5%BB%BA%E7%AB%8B%E5%B1%95%E9%96%8B%E5%BC%8F%E5%88%97%E8%A1%A8%E8%A6%96%E5%9C%96%E5%92%8C%E5%A4%A7%E7%B6%B1%E8%A6%96%E5%9C%96">第 28 章<br>如何建立展开式列表视图和大纲视图</h1>
<p>SwiftUI 列表视图 (list view) 和 UIKit 的 UITableView 很类似。在 SwiftUI 最初的版本中,Apple 的工程师已经把建立列表视图的过程变得轻而易举,我们不需要创建 prototype cell,也不需要委派 (delegate) 或 data source 的协议。我们只需要用几行代码,就可以使用客制化单元格来建构一个列表视图。</p>
<p>在 iOS 14 (和以上版本),Apple 继续改善列表视图,并添加了一些新功能。在本篇教学文章中,我们将会看看如何建构一个展开式列表视图 (expandable list view) 或大纲视图 (outline view),并探索 inset grouped 的列表样式 (list style)。</p>
<h3 id="%E7%AF%84%E4%BE%8B-app">示例 App</h3>
<p>首先,让我们看看本篇教学的成品。我十分喜欢 <a href="https://international.lamarzocco.com/en/" target="_blank">La Marzocco</a>,所以我用了它网站的导航菜单 (navigation menu) 为例子。以下的列表视图展示了菜单的大纲,使用者可以点击显示按钮来展开列表。</p>
<figure id="fig28.1"><img src="images/expandablelist/swiftui-expandablelist-1.jpg" alt="图 28.1. 展开式列表视图"><figcaption>图 28.1. 展开式列表视图</figcaption></figure>
<p>当然,你也可以用自己的实现方法,来建构这个大纲视图。但在最新版本的 SwiftUI 中,Apple 让开发者可以更简单地建构这种大纲视图,并自动适应于 iOS、iPadOS、和 macOS 版本。</p>
<h3 id="%E5%BB%BA%E6%A7%8B%E5%A4%A7%E7%B6%B1%E8%A6%96%E5%9C%96">建构大纲视图</h3>
<p>要继续阅读本章的教学的话,请先从 <a href="https://www.appcoda.com/resources/swiftui/expandablelist-images.zip" target="_blank">https://www.appcoda.com/resources/swiftui/expandablelist-images.zip</a> 下载示例图片。 然后,在 Xcode 使用 <em>App</em> 模板创建一个新的 SwiftUI 项目。 我将项目命名为 <em>SwiftUIExpandableList</em>,但您可以随意将名称设置为任何名称。</p>
<p>建立项目后,解压缩图像存档并将图像添加到 Assets。在项目导航器中,右键单击 <em>SwiftUIExpandableList</em> 并选择创建一个新文件档。 选择 <em>Swift File</em> 模板并将其命名为 <em>MenuItem.swift</em>。</p>
<h4 id="%E8%A8%AD%E7%BD%AE%E6%95%B8%E6%93%9A%E6%A8%A1%E5%9E%8B">设置数据模型</h4>
<p>要让列表视图可以展开,你只需要如此建立一个数据模型 (data model)。请加入以下代码至 <code>MenuItem.swift</code>:</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MenuItem</span>: <span class="hljs-title">Identifiable</span> </span>{
<span class="hljs-keyword">var</span> id = <span class="hljs-type">UUID</span>()
<span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>
<span class="hljs-keyword">var</span> image: <span class="hljs-type">String</span>
<span class="hljs-keyword">var</span> subMenuItems: [<span class="hljs-type">MenuItem</span>]?
}
</code></pre>
<p>在上面的代码中,我们有一个用来建造菜单物件的结构 (struct)。要创建一个巢状列表 (nested list),关键就是要加入一个属性,包含子级的可选数组 (optional array) <code>subMenuItems</code>。请注意,子级与其父级的类型 (type) 是一样的。</p>
<p>对于顶层 (top level) 菜单物件,我们可以如此创建一个 <code>MenuItem</code> 数组:</p>
<pre><code class="lang-swift"><span class="hljs-comment">// Main menu items</span>
<span class="hljs-keyword">let</span> sampleMenuItems = [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Espresso Machines"</span>, image: <span class="hljs-string">"linea-mini"</span>, subMenuItems: espressoMachineMenuItems),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Grinders"</span>, image: <span class="hljs-string">"swift-mini"</span>, subMenuItems: grinderMenuItems),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Other Equipment"</span>, image: <span class="hljs-string">"espresso-ep"</span>, subMenuItems: otherMenuItems)
]
</code></pre>
<p>我们会针对每个菜单物件,指定子菜单 (sub-menu) 物件的数组。如果没有子菜单物件,则可以省略 <code>subMenuItems</code> 参数,或传递 <code>nil</code> 值。我们可以这样定义子菜单物件:</p>
<pre><code class="lang-swift"><span class="hljs-comment">// Sub-menu items for Espressco Machines</span>
<span class="hljs-keyword">let</span> espressoMachineMenuItems = [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Leva"</span>, image: <span class="hljs-string">"leva-x"</span>, subMenuItems: [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Leva X"</span>, image: <span class="hljs-string">"leva-x"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Leva S"</span>, image: <span class="hljs-string">"leva-s"</span>) ]),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Strada"</span>, image: <span class="hljs-string">"strada-ep"</span>, subMenuItems: [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Strada EP"</span>, image: <span class="hljs-string">"strada-ep"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Strada AV"</span>, image: <span class="hljs-string">"strada-av"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Strada MP"</span>, image: <span class="hljs-string">"strada-mp"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Strada EE"</span>, image: <span class="hljs-string">"strada-ee"</span>) ]),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"KB90"</span>, image: <span class="hljs-string">"kb90"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Linea"</span>, image: <span class="hljs-string">"linea-pb-x"</span>, subMenuItems: [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Linea PB X"</span>, image: <span class="hljs-string">"linea-pb-x"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Linea PB"</span>, image: <span class="hljs-string">"linea-pb"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Linea Classic"</span>, image: <span class="hljs-string">"linea-classic"</span>) ]),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"GB5"</span>, image: <span class="hljs-string">"gb5"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Home"</span>, image: <span class="hljs-string">"gs3"</span>, subMenuItems: [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"GS3"</span>, image: <span class="hljs-string">"gs3"</span>), <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Linea Mini"</span>, image: <span class="hljs-string">"linea-mini"</span>) ])
]
<span class="hljs-comment">// Sub-menu items for Grinder</span>
<span class="hljs-keyword">let</span> grinderMenuItems = [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Swift"</span>, image: <span class="hljs-string">"swift"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Vulcano"</span>, image: <span class="hljs-string">"vulcano"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Swift Mini"</span>, image: <span class="hljs-string">"swift-mini"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Lux D"</span>, image: <span class="hljs-string">"lux-d"</span>)
]
<span class="hljs-comment">// Sub-menu items for other equipment</span>
<span class="hljs-keyword">let</span> otherMenuItems = [ <span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Espresso AV"</span>, image: <span class="hljs-string">"espresso-av"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Espresso EP"</span>, image: <span class="hljs-string">"espresso-ep"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Pour Over"</span>, image: <span class="hljs-string">"pourover"</span>),
<span class="hljs-type">MenuItem</span>(name: <span class="hljs-string">"Steam"</span>, image: <span class="hljs-string">"steam"</span>)
]
</code></pre>
<h4 id="%E9%A1%AF%E7%A4%BA%E5%88%97%E8%A1%A8">显示列表</h4>
<p>准备好数据模型后,我们可以编写代码以呈现列表视图。<code>List</code> 视图现在有了可选的 <code>children</code> 参数。所以如果有任何子物件,你可以提供其 key path <code>\.subMenuItems</code>。 然后,SwiftUI 将递归 (recursively) 查找子菜单物件,并以大纲形式显示。打开 <code>ContentView.swift</code> 并在 <code>body</code> 中插入以下代码:</p>
<pre><code class="lang-swift"><span class="hljs-type">List</span>(sampleMenuItems, children: \.subMenuItems) { item <span class="hljs-keyword">in</span>
<span class="hljs-type">HStack</span> {
<span class="hljs-type">Image</span>(item.image)
.resizable()
.scaledToFit()
.frame(width: <span class="hljs-number">50</span>, height: <span class="hljs-number">50</span>)
<span class="hljs-type">Text</span>(item.name)
.font(.system(.title3, design: .rounded))
.bold()
}
}
</code></pre>
<p>在 <code>List</code> 视图闭包中,我们会描述每一行的外观。在示例代码中,我们使用了 <code>HStack</code> 布局图像和文本视图。如果你已经在 <code>ContentView</code> 中正确地添加了代码的话,SwiftUI 应该会如此呈现大纲视图:</p>
<figure id="fig28.2"><img src="images/expandablelist/swiftui-expandablelist-2.png" alt="图 28.2. 展开式列表视图"><figcaption>图 28.2. 展开式列表视图</figcaption></figure>
<p>要测试App,请在模拟器或预览画布中运行它。 您可以点击披露指示器以显示子菜单。</p>
<h3 id="%E4%BD%BF%E7%94%A8%E6%99%AE%E9%80%9A%E5%88%97%E8%A1%A8%E6%A8%A3%E5%BC%8F">使用普通列表样式</h3>
<p>在 iOS 15 中,Apple 将列表视图的默认样式设置为 Inset Grouped,其中分组的部分以圆角嵌入。 如果要将其切换回普通列表样式,可以将 <code>.listStyle</code> 修饰器附加到 <code>List</code> 视图并将其值设置为 <code>.plain</code>,如下所示:</p>
<pre><code class="lang-swift"><span class="hljs-type">List</span> {
...
}
.listStyle(.plain)
</code></pre>
<p>如果你没有加错位置,列表视图现在应该修改为普通样式。</p>
<figure id="fig28.3"><img src="images/expandablelist/swiftui-expandablelist-3.png" alt="图 28.3. 使用普通列表样式"><figcaption>图 28.3. 使用普通列表样式</figcaption></figure>
<h3 id="%E4%BD%BF%E7%94%A8-inset-grouped-%E5%88%97%E8%A1%A8%E6%A8%A3%E5%BC%8F">使用 Inset Grouped 列表样式</h3>
<p>正如您在前面的示例中所见,使用 <code>List</code> 视图创建大纲视图非常容易。 但是,如果您想更好地控制大纲视图的外观(例如添加部分标题),则需要使用 <code>OutlineGroup</code>。 这个新视图是自 iOS 14 才引入的,用于呈现数据的层次结构。</p>
<p>如果您已明白如何构建可扩展的列表视图,“OutlineGroup”的用法也非常相似。 例如,以下代码允许您构建相同的可扩展列表视图,如图 28.1 所示:</p>
<pre><code class="lang-swift"><span class="hljs-type">List</span> {
<span class="hljs-type">OutlineGroup</span>(sampleMenuItems, children: \.subMenuItems) { item <span class="hljs-keyword">in</span>
<span class="hljs-type">HStack</span> {
<span class="hljs-type">Image</span>(item.image)
.resizable()
.scaledToFit()
.frame(width: <span class="hljs-number">50</span>, height: <span class="hljs-number">50</span>)
<span class="hljs-type">Text</span>(item.name)
.font(.system(.title3, design: .rounded))
.bold()
}
}
}
</code></pre>
<p>和 <code>List</code> 视图类似,您只需要在建立<code>OutlineGroup</code> 时传入要显示的项目并指定子菜单项(或子项)的键路径(Key path)。</p>
<p>有了 <code>OutlineGroup</code>,您可以更好地控制大纲视图的外观。 例如,我们希望将顶级菜单项显示为节标题。 你可以这样写代码:</p>
<pre><code class="lang-swift"><span class="hljs-type">List</span> {
<span class="hljs-type">ForEach</span>(sampleMenuItems) { menuItem <span class="hljs-keyword">in</span>
<span class="hljs-type">Section</span>(header:
<span class="hljs-type">HStack</span> {
<span class="hljs-type">Text</span>(menuItem.name)
.font(.title3)
.fontWeight(.heavy)
<span class="hljs-type">Image</span>(menuItem.image)
.resizable()
.scaledToFit()
.frame(width: <span class="hljs-number">30</span>, height: <span class="hljs-number">30</span>)
}
.padding(.vertical)
) {
<span class="hljs-type">OutlineGroup</span>(menuItem.subMenuItems ?? [<span class="hljs-type">MenuItem</span>](), children: \.subMenuItems) { item <span class="hljs-keyword">in</span>
<span class="hljs-type">HStack</span> {
<span class="hljs-type">Image</span>(item.image)
.resizable()
.scaledToFit()