Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

ios - SwiftUI List doesn't use lazy loading for first 20 elements. How to fix that?

We have the following row view. The screen has space to render about 4 rows.

struct SampleRow: View {
    let id: Int

    var body: some View {
        Text("Row (id)").frame(height: 200)
    }

    init(id: Int) {
        print("Loading row (id)")
        self.id = id
    }
}

Use LazyVStack to make a list

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(1...1000, id: .self, content: SampleRow.init)
            }
        }
    }
}

Result

Loading row 1
Loading row 2
Loading row 3
Loading row 4
Loading row 5

Use List to make a list

struct ContentView: View {
    var body: some View {
        List {
            ForEach(1...1000, id: .self, content: SampleRow.init)
        }
    }
}

Result

Loading row 1
Loading row 2
Loading row 3
Loading row 4
Loading row 5
Loading row 6
Loading row 7
Loading row 8
Loading row 9
Loading row 10
Loading row 11
Loading row 12
Loading row 13
Loading row 14
Loading row 15
Loading row 16
Loading row 17
Loading row 18
Loading row 19
Loading row 20

Even if I change the row height from 200 to 400 when the screen has space to render only 2 rows, List loads first 20 items in any case.

How to change this value?

question from:https://stackoverflow.com/questions/65869281/swiftui-list-doesnt-use-lazy-loading-for-first-20-elements-how-to-fix-that

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...