我使用 dislousreGroup 作为递归,但视图更新无法正常工作。有啥办法吗?
Posted
技术标签:
【中文标题】我使用 dislousreGroup 作为递归,但视图更新无法正常工作。有啥办法吗?【英文标题】:I use disclousreGroup as recursion, but the view update doesn't work properly. Is there any way?我使用 dislousreGroup 作为递归,但视图更新无法正常工作。有什么办法吗? 【发布时间】:2021-08-31 07:29:05 【问题描述】:enter image description here
如果折叠后更改数据,View被重用,绘制的很奇怪。
在 swiftUI 中重用视图似乎会产生这些结果。
我可以控制视图的重用吗?还是有别的办法?
和id有关吗?
下面是实现这个的代码。
import SwiftUI
@main
struct TestApp: App
var item: Item = dummyItems
var item2: Item = dummyItems2
@State var isChange: Bool = true
var body: some Scene
WindowGroup
VStack
List
if isChange
ContentView(item: item)
else
ContentView(item: item2)
Button("change")
isChange.toggle()
import SwiftUI
struct ContentView: View
let item: Item
init(item: Item)
self.item = item
var body: some View
ForEach(item.children) item in
if item.children.isEmpty
Text("\(item.title)")
else
DisclosureGroup(
isExpanded: item.$isExpanded,
content:
ContentView(item: item)
,
label: Text("\(item.title)")
)
struct ContentView_Previews: PreviewProvider
static var previews: some View
let item: Item = dummyItems
ContentView(item: item)
struct Item: Identifiable
var id: UUID = UUID()
@Binding var isExpanded: Bool
var title: String = "title"
var children: [Item] = []
init(title: String,
children: [Item])
self.title = title
self.children = children
self._isExpanded = .constant(true)
let dummyItems2 = Item(title: "root",
children: [
Item(title: "1",
children: [
Item(title: "1-1",
children: [
Item(title: "1-1-1",
children: [])]
),
Item(title: "1-2",
children: [
Item(title: "1-2-1",
children: []),
Item(title: "1-2-2",
children: [])]
)
]),
Item(title: "2",
children: [
Item(title: "2-1",
children: [
Item(title: "2-1-1",
children: [])]
),
Item(title: "2-2",
children: [
Item(title: "2-2-1",
children: [])]
),
Item(title: "2-3",
children: [
Item(title: "2-3-1",
children: [])]
),
]),
Item(title: "3",
children: [
Item(title: "3-1",
children: [
Item(title: "3-1-1",
children: []),
Item(title: "3-1-2",
children: []),
Item(title: "3-1-3",
children: [])]
),
Item(title: "3-2",
children: [
Item(title: "3-1-1",
children: [])]
),
Item(title: "3-3",
children: [
Item(title: "3-1-1",
children: [])]
)
])
])
let dummyItems = Item(title: "root",
children: [
Item(title: "1",
children: [
Item(title: "1-1",
children: [
Item(title: "1-1-1",
children: [])]
),
Item(title: "1-2",
children: [
Item(title: "1-2-1",
children: []),
Item(title: "1-2-2",
children: [])]
)
]),
Item(title: "2",
children: [
Item(title: "2-1",
children: [
Item(title: "2-1-1",
children: [])]
),
]),
Item(title: "3",
children: [
Item(title: "3-1",
children: [
Item(title: "3-1-1",
children: []),
Item(title: "3-1-2",
children: []),
Item(title: "3-1-3",
children: [])]
),
Item(title: "3-2",
children: [
Item(title: "3-1-1",
children: [])]
),
Item(title: "3-3",
children: [
Item(title: "3-1-1",
children: [])]
)
])
])
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:试试下面的
List
if isChange
ContentView(item: item)
else
ContentView(item: item2)
.id(isChange) // << here !!
【讨论】:
List ContentView(item: isChange ? item : item2) .id(isChange) 如果我这样更改,id 不起作用。还有其他方法吗?以上是关于我使用 dislousreGroup 作为递归,但视图更新无法正常工作。有啥办法吗?的主要内容,如果未能解决你的问题,请参考以下文章