struct HomeView: View
{
@State private var selectedItem: String?
var body: some View
{
VStack
{
List
{
DisclosureGroupWithButton(title: "Group 1", items: ["Item 1", "Item 2", "Item 3"])
DisclosureGroupWithButton(title: "Group 2", items: ["Item A", "Item B", "Item C"])
}
.listStyle(SidebarListStyle())
List(selection: self.$selectedItem)
{
ForEach(["Option 1", "Option 2", "Option 3"], id: \.self)
{
item in Text(item) .tag(item)
}
}
.frame(maxHeight: .infinity)
}
}
}
struct DisclosureGroupWithButton: View
{
let title: String
let items: [String]
@State private var isExpanded: Bool = false
var body: some View
{
DisclosureGroup(isExpanded: self.$isExpanded)
{
ForEach(items, id: \.self)
{
item in Text(item)
}
Button("Add Item")
{
print("\(title) button tapped")
}
.buttonStyle(.bordered)
.padding(.top, 5)
}
label:
{
Text(self.title)
}
}
}
我想做到这一点,所以底部的列表几乎直接直接在两个披露组下方,略有空间将它们分开(没有空白空间),但是我很难做到这一点。明显的解决方案是在列表之后使用垫片,但由于某种原因,这无能为力。我把所有元素的框架都弄乱了,没有任何改变。我还注意到,即使我将其剥离为简单的两个列表,也会发生相同的行为:
struct HomeView: View
{
@State private var selectedItem: String?
var body: some View
{
List
{
ForEach(["Option 1", "Option 2", "Option 3"], id: \.self)
{
item in Text(item) .tag(item)
}
}
List(selection: self.$selectedItem)
{
ForEach(["Option 1", "Option 2", "Option 3"], id: \.self)
{
item in Text(item) .tag(item)
}
}
}
}
没有人知道如何实现这一目标?
它似乎只需要一个带有2个部分的单个
List
。
List(selection: self.$selectedItem) {
Section {
DisclosureGroupWithButton(title: "Group 1", items: ["Item 1", "Item 2", "Item 3"])
DisclosureGroupWithButton(title: "Group 2", items: ["Item A", "Item B", "Item C"])
}
.selectionDisabled()
Section {
ForEach(["Option 1", "Option 2", "Option 3"], id: \.self) {
item in Text(item) .tag(item)
}
}
}
// .listSectionSpacing(...) // if you want to adjust the spacing
.listStyle(.sidebar)
.selectionDisabled()