SwiftUI 中免费的可扩展 List 有啥要求?

Posted

技术标签:

【中文标题】SwiftUI 中免费的可扩展 List 有啥要求?【英文标题】:What are the requirements for a free expandable List in SwiftUI?SwiftUI 中免费的可扩展 List 有什么要求? 【发布时间】:2021-03-14 13:37:09 【问题描述】:

在我的代码中的某处,我有这个非常标准的部分列表:

var body: some View 
    List 
        ForEach(userData.groupedBookings)  group in
            Section(header: Text(group.key)) 
                ForEach(group.items)  booking in
                    LessonRow(booking: booking)
                
            
        
    

不知何故,这些部分是可展开/可折叠的,这让我很高兴,但我不知道为什么。 我特别沮丧,因为我想用类似的代码在其他地方重现这种行为,而不是展开/折叠。

自动获取这个有什么要求?

【问题讨论】:

【参考方案1】:

它由侧边栏列表样式激活(在某些情况下被视为默认),您可以显式使用它

List 
    ForEach(userData.groupedBookings)  group in
        Section(header: Text(group.key)) 
            ForEach(group.items)  booking in
                LessonRow(booking: booking)
            
        
    

.listStyle(SidebarListStyle())

作为替代,您可以明确使用DisclosureGroup 来对部分进行披露行为,例如https://***.com/a/63228810/12299030

【讨论】:

谢谢。它很接近,但风格并不完全相同。我得到大节文本+清晰的列表背景,而我的代码得到小节文本+白色标题。我尝试了 DisclosureGroup,但在寻求一致性时也获得了另一种样式。 那是因为.navigationViewStyle(StackNavigationViewStyle())

以上是关于SwiftUI 中免费的可扩展 List 有啥要求?的主要内容,如果未能解决你的问题,请参考以下文章