【问题标题】:SwiftUi horizontal Scrollview with data from ApiSwiftUi 水平滚动视图,带有来自 Api 的数据
【发布时间】:2020-04-12 19:32:59
【问题描述】:

我需要使用从 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)

当我删除滚动视图时,数据正在完美加载。对此有什么建议吗?谢谢

【问题讨论】:

  • 将 .onAppear 修饰符移动到 ScrollView 而不是 HStack 时会发生什么?
  • @unequalsine 什么都没有,白屏
  • 一个 Apple 错误....我可以重现您的错误
  • @Chris 这个错误有什么解决方案吗?
  • 只是一种解决方法:在显示视图之前加载图像....

标签: ios swift swiftui scrollview swiftui-list


【解决方案1】:

试试这个:

这是一个快速而肮脏的例子... ;)

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2015-06-10
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多