私はTimerを使ってIntをインクリメントしています。ボタンを押すと、現在の数値が配列に記録されます。また、配列の内容を表示するTableViewもあります。私がTableViewをつかんでそれを少しでもスクロールすると、TableViewを解放するまでタイマーがハングするという点を除いて、すべてうまく動作します。Swift 3.2 - TableViewを使ったTimer()
私はTableViewに何をしていても、タイマーを実行し続けることができますか?ここで
私ViewController.swiftです:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var myTableView: UITableView!
var timer = Timer()
var isTimerOn = false
var counter = 0
var samples: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
}
@IBAction func startStopButton(_ sender: Any) {
switch isTimerOn {
case true:
timer.invalidate()
isTimerOn = false
case false:
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
self.counter += 1
print(self.counter)
}
isTimerOn = true
}
}
@IBAction func recordButton(_ sender: Any) {
samples.insert(counter, at: 0)
myTableView.reloadData()
}
}
extension ViewController {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return samples.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.number = samples[indexPath.row]
return cell
}
}
あなたがそれを必要とする場合は、上のプロジェクト全体を見ることができます: https://github.com/Fr3qu3ntFly3r/TimerTest
事前に助けをいただき、ありがとうございます。
このページをチェックしhttps://stackoverflow.com/questions/29204229/countdown-timer-on-uitableviewcell-scrolling-laggy-issue-of -uitableview – Rex