如何在Swift中的UITableView底部显示"pull-to-refresh"元素

Fro*_*art 10 uitableview ios swift

我需要在以下Objective-C库UIRefreshControl的底部显示UITableView:

但我正在使用Swift,并在使用"Bridging-Header.h"这些库的文件时注意到一些问题.

实现此类行为的替代方案和最简单方法是什么?

提前致谢.

Car*_*ann 6

我在我的项目中遇到了同样的问题,并找到了这个答案。您可以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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Thi*_*guy 0

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...")
}
Run Code Online (Sandbox Code Playgroud)