在 SwiftUI 中下拉刷新数据 [英] Pull down to refresh data in SwiftUI

查看:14
本文介绍了在 SwiftUI 中下拉刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 List 使用了简单的数据列表.我想添加下拉刷新功能,但我不确定哪种方法是最好的.

i have used simple listing of data using List. I would like to add pull down to refresh functionality but i am not sure which is the best possible approach.

下拉刷新视图只会在用户尝试从第一个索引下拉时可见,就像我们在 UITableViewUIRefreshControlUIKit 中所做的一样

Pull down to refresh view will only be visible when user tries to pull down from the very first index same like we did in UITableView with UIRefreshControl in UIKit

这是在 SwiftUI 中列出数据的简单代码.

Here is simple code for listing data in SwiftUI.

struct CategoryHome: View {
    var categories: [String: [Landmark]] {
        .init(
            grouping: landmarkData,
            by: { $0.category.rawValue }
        )
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(categories.keys.sorted().identified(by: .self)) { key in
                    Text(key)
                }
            }
            .navigationBarTitle(Text("Featured"))
        }
    }
}

推荐答案

我正在玩的应用程序需要同样的东西,而且看起来 SwiftUI API 不包含 的刷新控制功能此时 ScrollViews.

I needed the same thing for an app I'm playing around with, and it looks like the SwiftUI API does not include a refresh control capability for ScrollViews at this time.

随着时间的推移,API 将开发并纠正这些类型的情况,但 SwiftUI 中缺少功能的一般后备将始终实现一个实现 UIViewRepresentable 的结构.这是带有刷新控件的 UIScrollView 的快速而肮脏的.

Over time, the API will develop and rectify these sorts of situations, but the general fallback for missing functionality in SwiftUI will always be implementing a struct that implements UIViewRepresentable. Here's a quick and dirty one for UIScrollView with a refresh control.

struct LegacyScrollView : UIViewRepresentable {
    // any data state, if needed

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> UIScrollView {
        let control = UIScrollView()
        control.refreshControl = UIRefreshControl()
        control.refreshControl?.addTarget(context.coordinator, action:
            #selector(Coordinator.handleRefreshControl),
                                          for: .valueChanged)

        // Simply to give some content to see in the app
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
        label.text = "Scroll View Content"
        control.addSubview(label)

        return control
    }


    func updateUIView(_ uiView: UIScrollView, context: Context) {
        // code to update scroll view from view state, if needed
    }

    class Coordinator: NSObject {
        var control: LegacyScrollView

        init(_ control: LegacyScrollView) {
            self.control = control
        }

        @objc func handleRefreshControl(sender: UIRefreshControl) {
            // handle the refresh event

            sender.endRefreshing()
        }
    }
}

但当然,如果不将它们包装在 UIHostingController 中并将它们放入 makeUIView 中,而不是将它们放入,你就不能在滚动视图中使用任何 SwiftUI 组件a LegacyScrollView() {//在这里查看 }.

But of course, you can't use any SwiftUI components in your scroll view without wrapping them in a UIHostingController and dropping them in makeUIView, rather than putting them in a LegacyScrollView() { // views here }.

这篇关于在 SwiftUI 中下拉刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆