How do you implement list paging in SwiftUI or infinite list view?

核能气质少年 提交于 2021-02-04 19:42:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!