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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…