pierreafranck
pierreafranck

Reputation: 443

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.

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".

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 !

Thanks for your time.

P.S: I can post some code but I don't think it's really necessary for my question.

EDIT:

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

Upvotes: 2

Views: 710

Answers (1)

Adrian Bobrowski
Adrian Bobrowski

Reputation: 2794

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.

Upvotes: 2

Related Questions