如何在 SwiftUI 或无限列表视图中实现列表分页? [英] How do you implement list paging in SwiftUI or infinite list view?
本文介绍了如何在 SwiftUI 或无限列表视图中实现列表分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要有关如何在 SwiftUI 中实现无限列表滚动或分页列表的帮助.提前致谢
I want help on how to implement infinite list scrolling or paging list in SwiftUI.Thanks in advance
推荐答案
最好的办法是使用 .onAppear
并计算是否是时候获取下一页.这是一个人为的例子,因为通常您正在访问比这慢得多的网络或磁盘,但它会给您一个想法.针对您的特定用例调整 getNextPageIfNecessary(_:)
.
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))
}
这篇关于如何在 SwiftUI 或无限列表视图中实现列表分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文