滚动时滚动视图中作为列表项的内容消失 (swiftui),为什么? [英] Content in scrollview as a list item disappears when scrolling (swiftui), why?

查看:54
本文介绍了滚动时滚动视图中作为列表项的内容消失 (swiftui),为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Asperi 的解决方案,ScrollView().id(UUID().uuidString).

Solution by @Asperi, ScrollView().id(UUID().uuidString).

列表项中有滚动视图,当我滚动列表时,滚动视图中的内容消失了.

There is a scrollview in the list item, when I scroll the list, the content in scrollview disappears.

我认为问题是关于滚动视图 &列出可重复使用的项目冲突.

I think the issue is about scrollview & list reusable item conflict.

如果我删除滚动视图(只是 hstack{}),则什么都不会消失.所以我认为这是滚动视图的问题.任何理想?

If I remove scrollview(just hstack{}), nothing disappears. So I think it's scrollview's issue. Any ideal?

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0...100, id: \.self) { _ in
                ItemView().padding()
            }
        }
    }
}

struct ItemView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Tag list:")
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(0...8, id: \.self) { _ in
                        TagView1()
                    }
                }
            }.id(UUID().uuidString) /// <- fix
        }
    }
}

struct TagView1: View {
    var body: some View {
        Text("Tag\(String(UUID().uuidString.prefix(5)))")
            .foregroundColor(.secondary)
            .padding(.horizontal, 2)
            .background(RoundedRectangle(cornerRadius: 4).stroke(Color.secondary.opacity(0.5)))
            .padding(1)
    }
}

推荐答案

如果我正确理解了什么消失了(我假设你的意思是垂直滚动时列出行),那么是的,这是由于 List 重用/cache 优化问题.

If I correctly understood what disappears (I assume you meant list rows on vertical scrolling), then yes it is due to List reuse/cache optimisation issue.

以下方法应该有效(使用 Xcode 11.2/iOS 13.2 测试)

The following approach should work (tested with Xcode 11.2 / iOS 13.2)

struct ItemView: View {
    var body: some View {
        VStack {
            Text("Tag list:")
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(0...8, id: \.self) { _ in
                        TagView().padding()
                    }
                }
            }.id(UUID().uuidString) // << here is a fix !
        }
    }
}

这篇关于滚动时滚动视图中作为列表项的内容消失 (swiftui),为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆