【发布时间】:2020-03-09 21:10:35
【问题描述】:
我在使用 SwiftUI 的 Swift5 项目中有一个视图。 这是一个列表视图。
有没有办法根据变量的数据在列表中添加或删除 VStack?
我无法在这部分代码中放置任何逻辑,所以现在我很挣扎。
List {
VStack {
Text(txt)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
}
VStack { // this view should be displayed or hidden if the data variable is 0
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .bottomLeading)
}.onReceive(imageLoader.didChange) { data in
self.image = UIImage(data: data) ?? UIImage()
// here I check if there is any kind of data
if data.count == 0 {
print("Data is 0 so there is no image") // in this case I don't need the second VStack
} else {
print("Data is not 0 so there is an image") // in this case I need the second VStack
}
}
}
我从来没有尝试过学习 SwiftUI,因为我习惯了 Swift5,所以我没有任何 SwiftUI 知识。
【问题讨论】:
标签: swift swiftui swiftui-list