0

I want to trigger refreshControl to show automatically when a tableView is initializing its data from server, and I have tried several ways in this question

UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController

The refreshControl did show when the data is being initialized at the begining, but it spins too fast(maybe 2x the normal speed).

Does anyone know the reason and how to solve this problem?

Community
  • 1
  • 1

1 Answers1

0

Here is the code

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: "下拉刷新数据")
    }
}
  • Why you begin refreshControl in viewWillAppear method? You can trigger the refreshControl in top of refreshData method. viewWillAppear method is called more than once. – Ugur Atci Mar 24 '18 at 21:02