我的异步图像有问题。
这是文件的链接 https://www.vadimbulavin.com/asynchronous-swiftui-image-loading-from-url-with-combine-and-swift/
底部栏的图像不会因按钮操作而改变。我该如何解决它,你能解释一下吗?
podcastIndex 是 PodcastParser 类中的 @Publisched
文字改变也没有问题,但图像始终是一样的。
这是我的内容视图:
struct ContentView: View {
@ObservedObject var podcastParser = PodcastParser()
init() {
podcastParser.loadData()
}
var body: some View {
NavigationView {
List(podcastParser.podcasts.indices) { index in
HStack {
Button(action: {
podcastParser.podcastIndex = index
print(podcastParser.podcasts[podcastParser.podcastIndex].imageUrl)
}) {
HStack {
AsyncImage(
url: podcastParser.podcasts[index].imageUrl,
placeholder: {
Text("Loading...")
},
image: { Image(uiImage: $0).resizable() }
)
.scaledToFit()
.frame(width: 50, height: 50)
Text(podcastParser.podcasts[index].name)
}
}
}
}
}
VStack {
HStack {
AsyncImage(
url: podcastParser.podcasts[podcastParser.podcastIndex].imageUrl,
placeholder: { …
Run Code Online (Sandbox Code Playgroud)