【问题标题】:How to show "pull-to-refresh" element at the bottom of the UITableView in Swift如何在 Swift 中的 UITableView 底部显示“下拉刷新”元素
【发布时间】:2015-01-27 05:48:53
【问题描述】:

我需要在UITableView 的底部显示UIRefreshControl,就像在以下Objective-C 库中一样:

但我使用的是 Swift,并注意到在将 "Bridging-Header.h" 文件与这些库一起使用时出现了一些问题。

实现这种行为的替代方法和最简单的方法是什么?

提前致谢。

【问题讨论】:

  • 我也在使用第二个库和 Swift,没有任何问题。你有什么问题?您可能需要详细说明。
  • 我正在使用 CCBottomPullToRefresh,当我拉动时方法触发,但活动指示器不显示。 @乔黄

标签: ios uitableview swift


【解决方案1】:

我在我的项目中遇到了同样的问题,并找到了this answer。你可以在表格底部添加一个UIActivityIndicatorView实例,最初是隐藏的,当它进入上面的if条件时,取消隐藏并开始动画。

您可能需要更改表格的底部偏移量,在加载时为其添加“1 个单元格”高度,并在 if 条件内完成时将其放回。

在 Swift 3 中:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    // Use this 'canLoadFromBottom' variable only if you want to load from bottom iff content > table size
    let contentSize = scrollView.contentSize.height
    let tableSize = scrollView.frame.size.height - scrollView.contentInset.top - scrollView.contentInset.bottom
    let canLoadFromBottom = contentSize > tableSize

    // Offset
    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
    let difference = maximumOffset - currentOffset

    // Difference threshold as you like. -120.0 means pulling the cell up 120 points
    if canLoadFromBottom, difference <= -120.0 {

        // Save the current bottom inset
        let previousScrollViewBottomInset = scrollView.contentInset.bottom
        // Add 50 points to bottom inset, avoiding it from laying over the refresh control.
        scrollView.contentInset.bottom = previousScrollViewBottomInset + 50

        // loadMoreData function call
        loadMoreDataFunction(){ result in
            // Reset the bottom inset to its original value
            scrollView.contentInset.bottom = previousScrollViewBottomInset
        }
    }
}

【讨论】:

  • 谢谢你,这很有帮助,而不是使用 scrollViewDidScroll
【解决方案2】:
var refreshControl: UIRefreshControl!

//Pull to refresh
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "pullToRefresh:", forControlEvents: UIControlEvents.ValueChanged)
self.messageTableView.addSubview(refreshControl)

//Refresh the table view (pull to refresh)
func pullToRefresh(sender:AnyObject)
{
    print("pull to refresh...")
}

【讨论】:

  • 完全不是《冰雪奇缘》所要求的,对吧?这是顶部的标准刷新控件。
猜你喜欢
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 2014-10-23
  • 2018-09-21
  • 2017-11-14
相关资源
最近更新 更多