ios - SwiftUi 水平滚动视图,带有来自 Api 的数据
问题描述
我需要使用从 api 加载的数据制作一个水平滚动视图。我有这样的看法:
ScrollView(.horizontal,showsIndicators:false) {
HStack(spacing: 20) {
ForEach(self.images, id: \.self) { item in
URLImage(URL(string:item)!)
{ proxy in
proxy.image
.resizable()
}.frame(width: UIScreen.main.bounds.width - 120, height: 200).aspectRatio(contentMode: .fit)
.cornerRadius(15)
.opacity(0.7)
}
}.onAppear(perform: loadData)
}.padding(.all,20)
当我删除滚动视图时,数据正在完美加载。对此有什么建议吗?谢谢
解决方案
试试这个:
这是一个快速而肮脏的例子......;)
var imageLoader = ImageLoader()
class ImageLoader {
var images : [Image] = []
init() {
loadData()
}
func loadData() {
for _ in 0..<10 {
images.append( Image(systemName: "circle.fill"))
}
}
}
struct ContentView: View {
var body: some View {
ScrollView(.horizontal,showsIndicators:false) {
HStack(spacing: 20) {
ForEach(0..<imageLoader.images.count, id: \.self) { item in
imageLoader.images[item]
.resizable()
.frame(width: UIScreen.main.bounds.width - 120, height: 200).aspectRatio(contentMode: .fit)
.cornerRadius(15)
.opacity(0.7)
}
}.background(Color.yellow)
}.background(Color.orange)
}
}
推荐阅读
- windows-10 - 如何在 Windows 10 中获取未保存的打印屏幕图片?
- android - 自定义 Android 自动填充服务不会出现在自动填充应用中
- c# - 没有任何 c# 编译就重定向到另一个网页的 aspx 文件的完整文件内容是什么?
- android - 带有前缀和哈希字符串的 Android 短信验证消息
- html - 如何将带有查询参数的 URL 作为查询参数传递给另一个 URL?
- html - 从 div 中平滑删除动画
- c# - Autofac 错误地解析命名服务
- mysql - 从具有单列的表中删除重复记录
- python - 如何在pygame中发出挥之不去的消息
- c - Visual Studio 代码调试 C 请求超时