UIRefreshControl 无法正常工作

UIRefreshControl does not work correctly

刷新轮在 95% 满时停止,只有从屏幕顶部到达底部时才会刷新。

lazy var refreshControl: UIRefreshControl = {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action:
       #selector(VFeedVC.refresh(_:)),
                            for: .valueChanged)
    return refreshControl
}()

override func viewDidLoad() {
    self.tableView.addSubview(refreshControl)
}

func refresh(_ sender: Any) {
    Metric.sendEvent("RefreshFeed")
    self.refreshControl.beginRefreshing()
    self.getFeeds(self.user.id)
}

func getFeeds(_ userId: Int) {
    VNetwork.getFeeds(userId) { feeds in
        self.feeds = feeds
        self.tableView.isHidden = self.feeds.count == 0
        self.configureCells()
        self.refreshControl.endRefreshing()
        self.tableView.reloadData()
    }
}

这适用于我 iOS 11:

override func viewDidLoad() {
    self.tableView.refreshControl = refreshControl
    self.tableView.addSubview(refreshControl)
}

此外,您不需要明确告诉刷新控件开始刷新,所以这就足够了:

func refresh(_ sender: Any) {
    Metric.sendEvent("RefreshFeed")
    self.getFeeds(self.user.id)
}

还要确保在 VNetwork.getFeeds(userId) { feeds in 的回调中你在主线程上。

使用self.tableView.refreshControl = refreshControl你可以将refreshControl直接添加到tableview,你也不必调用self.refreshControl.beginRefreshing()

lazy var refreshControl: UIRefreshControl = {
   let refreshControl = UIRefreshControl()
   refreshControl.addTarget(self, action:
       #selector(VFeedVC.refresh(_:)),
                            for: .valueChanged)
   return refreshControl
}()
override func viewDidLoad() {

self.tableView.refreshControl = refreshControl
self.tableView.addSubview(refreshControl)
}
func refresh(_ sender: Any) {
    Metric.sendEvent("RefreshFeed")
    self.getFeeds(self.user.id)
}

func getFeeds(_ userId: Int) {
    VNetwork.getFeeds(userId) { feeds in
        self.feeds = feeds
        self.tableView.isHidden = self.feeds.count == 0
        self.configureCells()
        self.refreshControl.endRefreshing()
        self.tableView.reloadData()
    }
}

试试这个对我有用。

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
        refreshControl = UIRefreshControl()
        refreshControl.tintColor = UIColor.white
        self.tblPartyList.addSubview(refreshControl)
        refreshControl.addTarget(self, action:#selector(handleRefresh(_:)), for: UIControlEvents.valueChanged)
}
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {

}

像这样把 refreshControl.endRefreshing() 放在 tableView.reloadData() 之后:

tableView.reloadData()
DispatchQueue.main.async {
    self.refreshControl.endRefreshing()
}