滚动时如何在计时器上更新UITableView [英] How do I update a UITableView on a timer while scrolling
问题描述
我的问题很难解释,所以我会尽力解释.
My problem is very complicated to explain so I will do my best for explain it.
我正在使用TableView做一个快速的应用程序.在此TableView中,我有一些存储在本地的数据(字典,数组,var,...). 因此,在TableView中,我每0.01秒刷新一次此数据.然后,当我滚动TableView时,此刷新将停止,并且我不希望这样做.我想要继续刷新".
I'm doing a swift application with TableView. In this TableView, I have some data which are store in local (Dictionnary, Arrays, var, ...). So, in my TableView I'm refreshing this datas every 0.01 second. Then, when I scroll my TableView this refresh is stopped and I don't want it. I Want a "continue refresh".
有人可以解释我该怎么做?我在StackOverflow上搜索,最大的答案是:线程. 我了解C语言中的Thread,但是在Swift中对我来说却非常模糊. 如果您有一个在Swift中训练多线程的练习,则可以发布它!
Someone can explain how I can do it? I search on StackOverflow and the most answer is : the Thread. I understood Thread in C but it's very vague for me in Swift. If you have an exercise for train multithreading in Swift you can post it !
感谢您的宝贵时间.
P.S:我可以发布一些代码,但我认为我的问题确实没有必要.
P.S: I can post some code but I don't think it's really necessary for my question.
有用于计时器和更新的代码
There is the code for the timer and update
override func viewDidLoad() {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self,
selector:#selector(ViewController.updateTimer), userInfo: nil,
repeats: true)
}
func updateTimer () {
var j = 0
for _ in rows {
if (rows[j]["Activ"] as! Bool == true ) {
rows[j]["timer"] = (rows[j]["timer"] as! Double + 0.01) as AnyObject
}
j += 1
}
myTableView.reloadData()
}
推荐答案
因此,之所以发生是因为Timer
在与UITableView
滚动相同的DispatchQueue
中工作.您可以使用例如 AsyncTimer 解决此问题.
So, it happens because the Timer
works in this same DispatchQueue
as scrolling in the UITableView
. You can solve this problem by using, for example, AsyncTimer.
这篇关于滚动时如何在计时器上更新UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!