UITableView下拉刷新页面的实现

很多移动应用都使用了下拉列表,列表就重新加载数据的应用。下面总结下UITableView下拉刷新页面的实现。其实有开源代码,实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh,将其中的resources和classes文件拷贝到工程目录下,然后根据所给的demo,添加的相应的方法即可,这里就不多说,大家看看Demo就知道很简单了。

最后需要特别说明的是工程需要添加QuartzCore.framework。一些显示的文字和样式可以通过修改EGORefreshTableHeaderView.m实现。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 SwiftUI 中,可以使用 `List` 或 `ScrollView` 来显示可滚动的列表或视图。为了添加下拉刷新功能,可以使用 `UIRefreshControl`。 首先,需要创建一个 `UIRefreshControl` 的实例,然后将其添加到 `List` 或 `ScrollView` 中。在用户下拉列表时,`UIRefreshControl` 会触发一个动作。在该动作中,可以执行更新数据的操作,并结束下拉刷新。 下面是一个示例代码,演示如何在 SwiftUI 中使用 `UIRefreshControl` 实现下拉刷新: ```swift import SwiftUI struct ContentView: View { @State private var isRefreshing = false @State private var data = ["Item 1", "Item 2", "Item 3"] var body: some View { List(data, id: \.self) { item in Text(item) } .onPullToRefresh(isRefreshing: $isRefreshing) { // Simulate a network request DispatchQueue.main.asyncAfter(deadline: .now() + 2) { self.data = ["New Item 1", "New Item 2", "New Item 3"] self.isRefreshing = false } } } } extension View { func onPullToRefresh(isRefreshing: Binding<Bool>, action: @escaping () -> Void) -> some View { ModifiedContent(content: self, modifier: PullToRefresh(isRefreshing: isRefreshing, action: action)) } } struct PullToRefresh: ViewModifier { @Binding var isRefreshing: Bool var action: () -> Void func body(content: Content) -> some View { content .overlay( GeometryReader { geometry in VStack { if self.isRefreshing { ActivityIndicator(isAnimating: .constant(true), style: .medium) } else { Color.clear } } .frame(width: geometry.size.width, height: 60, alignment: .center) .offset(y: -60) }, alignment: .bottom ) .onAppear { let refreshControl = UIRefreshControl() refreshControl.addTarget(self, action: #selector(self.onRefresh), for: .valueChanged) UITableView.appearance().refreshControl = refreshControl } } @objc private func onRefresh() { isRefreshing = true action() } } struct ActivityIndicator: UIViewRepresentable { @Binding var isAnimating: Bool let style: UIActivityIndicatorView.Style func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView { UIActivityIndicatorView(style: style) } func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) { isAnimating ? uiView.startAnimating() : uiView.stopAnimating() } } ``` 在该示例中,我们使用 `List` 显示一个简单的字符串列表。我们添加了一个名为 `onPullToRefresh` 的自定义修饰符,用于在 `List` 上添加下拉刷新功能。该修饰符接受两个参数:一个布尔绑定,用于控制刷新控件的状态;一个闭包,用于执行更新数据的操作。 在该示例中,我们使用 `GeometryReader` 将一个 `ActivityIndicatorView` 添加到列表头部。当用户下拉列表时,`UIRefreshControl` 会触发一个动作,在该动作中,我们将布尔绑定设置为 `true`,然后执行更新数据的操作。当数据更新完成后,我们将布尔绑定设置为 `false`,结束下拉刷新。 请注意,由于 SwiftUI 中的 `List` 是基于 `UITableView` 实现的,因此我们需要使用 `UITableView` 的 `refreshControl` 属性来添加 `UIRefreshControl`。在 `onAppear` 中,我们设置了 `UITableView` 的 `refreshControl` 属性,将其与我们创建的 `UIRefreshControl` 实例关联起来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值