UIRefreshControl旋转速度太快

时间:2017-05-20 15:05:35

标签: ios swift

我想触发refreshControl在tableView从服务器初始化数据时自动显示,我在这个问题上尝试了几种方法

UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController

refreshControl确实显示数据在开始时被初始化,但它旋转得太快(可能是正常速度的2倍)。

有谁知道原因以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是代码

override func viewDidLoad() {
    super.viewDidLoad()
    networkManager = BookNetworkManager(userAccount: userAccount)
    HUD = JGProgressHUD.standard
    refreshControl!.addTarget(self, action: #selector(refreshData), for: .valueChanged)
    tableView.tableFooterView = UIView()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    delegate?.setNavgationItem(status: .none)
    if !loaded {
        tableView.setContentOffset(CGPoint(x: 0, y: -self.refreshControl!.height - self.topLayoutGuide.length), animated: true)
        refreshControl!.beginRefreshing()
        refreshData()
        loaded = true
    }
}

func refreshData() {
    delegate?.hidePlaceHolder(type: .history)
    refreshControl!.attributedTitle = NSAttributedString(string: "刷新数据中")
    networkManager.getHistoryBook()
        { (response, errorInfo, books) in
            if response == .success {
                self.historyBookList = books
                self.historyBookList.sort{ $0.returnDateTimeInterval > $1.returnDateTimeInterval }
                self.tableView.reloadData()
                if books.isEmpty {
                    self.delegate?.showPlaceHolder(message: "还没有借阅过书籍噢", for: .history)
                }else{
                    self.delegate?.hidePlaceHolder(type: .history)
                }
            }else {
                print(errorInfo?.errorDescription ?? response.rawValue)
                self.HUD.show(message: errorInfo?.errorDescription ?? response.rawValue, time: 2)
                self.delegate?.showPlaceHolder(message: "出错啦,下拉刷新一下吧?", for: .history)
            }
            self.refreshControl!.endRefreshing()
            self.refreshControl!.attributedTitle = NSAttributedString(string: "下拉刷新数据")
    }
}