UIRefreshControl无法正常工作

时间:2018-01-30 08:11:16

标签: ios swift uirefreshcontrol

刷新轮停在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()
    }
}

enter image description here

4 个答案:

答案 0 :(得分:1)

这适用于iOS 11:

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

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

VNetwork.getFeeds(userId) { feeds in

还要确保在prt.Contains(item) 的回调中你在主线程上。

答案 1 :(得分:0)

使用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()
    }
}

答案 2 :(得分:0)

put refreshControl.endRefreshing()像这样的tableView.reloadData()之后:

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

答案 3 :(得分:-1)

试试这个对我有用。

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) {

}