gpt4 book ai didi

ios - 加载 tableView 后停止事件指示器

转载 作者:可可西里 更新时间:2023-11-01 01:21:03 24 4
gpt4 key购买 nike

我在尝试停止我的事件指示器时遇到困难,“self.removeLoadingScreen()”应该放在哪里?它在 TableView 加载数据后无限旋转,我认为将 removeLoadingScreen() 放在 animateTable 函数下是理想的,但这是不正确的。

   var refresher: UIRefreshControl!

func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()
myTableView.setContentOffset(CGPoint(0, myTableView.contentOffset.y - refresher.frame.size.height), animated: true)

let qualityOfServiceClass = QOS_CLASS_BACKGROUND

let backgroundQueue = DispatchQueue.global(Int(qualityOfServiceClass.rawValue), 0)
backgroundQueue.async(execute: {[unowned self] in
//fetch you data here. you are now on a background queue
//to quick test it, uncomment the code below
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}

DispatchQueue.main.async(execute: { () -> Void in
self.myTableView.reloadData()
self.refresher.endRefreshing()
})
})

}
func refreshData(sender: UIRefreshControl) {


refresher.endRefreshing()
myTableView.reloadData()

}

最佳答案

从 setLoadingScreen 中删除这些行:

self.spinner.startAnimating()
self.myTableView.addSubview(loadingView)

并将它们添加到 refreshData:

func refreshData(sender: UIRefreshControl) {

myTableView.addSubview(loadingView) //here
spinner.startAnimating() //also, start the spinner
myTableView.reloadData()
refreshControl.endRefreshing()
}

它应该按预期工作。发生的事情是,在 viewDidLoad 中,当您调用 setLoadingScreen 时,您不仅设置了 loadingView,而且还将其添加到 View 层次结构中(从而显示它)。

此外,正如 Jitendra 所说,不要忘记在完成后删除 loadingView。

后期编辑:如果您只需要一个普通的 refreshControl,无需自定义,则可以去掉大部分代码。您只需要:

声明:

 var refresher: UIRefreshControl!

在viewDidLoad中:

    refresher = UIRefreshControl()
refresher.addTarget(self, action: "refreshData:", forControlEvents: .ValueChanged)
tableView.addSubview(refresher)

在刷新数据中:

  //get the new data
refresher.endRefreshing()
tableView.reloadData()

您不需要“loadingView”、“spinner”、“loadingLabel”,也不需要“setLoadingScreen”和“removeLoadingScreen”函数。 UIRefreshControl 将为您完成所有工作。让我知道它是否有效。

稍后再编辑(Swift 2)

如果您还想在第一次显示表格时以编程方式启动刷新控件,并在获取数据时让它运行:

func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()
tableView.setContentOffset(CGPointMake(0, tableView.contentOffset.y - refresher.frame.size.height), animated: true)

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {[unowned self] in
//fetch you data here. you are now on a background queue
//to quick test it, uncomment the code below
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
self.refresher.endRefreshing()
})
})

}

在 viewWillAppear 中调用这个函数。并去掉 animateTable 函数,除非你真的想要逐行动画的效果,在这种情况下:

  1. 你要么让它保持原样,它不会很明显(实际上,根本不可见......),但它会稍微减慢你的 UI

  2. 或者,如果您想慢慢来,并且想用响应性换取特效,则必须设置一个完成处理程序并在前一个完成的动画之后对每一行进行动画处理,这是另一个话题。

希望对你有帮助:)

编辑最后的编辑,翻译成 Swift 3

func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()

tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentOffset.y - refresher.frame.size.height), animated: true)

let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgroundQueue.async(execute: {[unowned self] in
//fetch you data here. you are now on a background queue
//to quick test it, uncomment the code below and modify the name of the array to match the array you are using
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}
//sleep(5) //alternately, you can use this to test. don't forget to remove this sleep line after you test

DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
self.refresher.endRefreshing()
})
})

}

关于ios - 加载 tableView 后停止事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com