问题
I want help on how to implement infinite list scrolling or paging list in SwiftUI.Thanks in advance
回答1:
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))
}
来源:https://stackoverflow.com/questions/58103707/how-do-you-implement-list-paging-in-swiftui-or-infinite-list-view