I want help on how to implement infinite list scrolling or paging list in SwiftUI.Thanks in advance
Your best bet is to use .onAppear
and calculate if it's time to fetch your next page. This is a contrived example because typically you're hitting a network or disk which is much slower than this, but it will give you an idea. Tune getNextPageIfNecessary(_:)
for your particular use-case.
@State var rows: [String] = Array(repeating: "Item", count: 20)
var body: some View {
List(0..<rows.count, id: \.self) { index in
Text(verbatim: self.rows[index])
.onAppear {
self.getNextPageIfNecessary(encounteredIndex: index)
}
}
}
private func getNextPageIfNecessary(encounteredIndex: Int) {
guard encounteredIndex == rows.count - 1 else { return }
rows.append(contentsOf: Array(repeating: "Item", count: 20))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With