我正在尝试为每个tableview单元格添加一个计时器。 我已启用分页滚动,我的tableview的每个单元格是全屏的。
我想要什么: 当用户在某个单元格上时,计时器会自动启动,并且当他们滚动时,该计时器会停止,而下一个单元格的另一个会启动...等
我还想记录过去的时间。
我尝试过的方法: 向视图控制器而不是tableview添加计时器,我有2个问题:
1)向上滑动计时器不会重置,尽管我使它无效,如下所示。
2)计时器在第一个单元格从服务器加载其内容之前启动
var timer = Timer()
var counter = 0
@IBOutlet var countingLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
countingLabel.text = String(counter)
timer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector: #selector(self.updateCounter), userInfo: nil, repeats: true)
// Do any additional setup after loading the view, typically from a nib.
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
performSegue(withIdentifier: "toSettings", sender: self)
} else if gesture.direction == UISwipeGestureRecognizer.Direction.up {
print("Swipe Up")
timer.invalidate()
counter = 0
countingLabel.text = String(counter)
timer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector: #selector(self.updateCounter), userInfo: nil, repeats: true)
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.down {
print("Swipe Down")
}
}
@objc func updateCounter() {
counter += 1
countingLabel.text = String(counter)
}
我希望为表视图的每个单元格触发一个计时器,并且我希望将经过的时间保留在可以在其他地方使用的变量中
答案 0 :(得分:0)
以下是您在每个timers
中处理UITableViewCell
的方式。
创建自定义UITableViewCell
class CustomCell: UITableViewCell {
//MARK: Outlets
@IBOutlet weak var label: UILabel!
//MARK: Internal Properties
var handler: ((Int)->())?
//MARK: Private Properties
private var timer: Timer?
private var counter = 0 {
didSet {
DispatchQueue.main.async {
self.label.text = "\(self.counter)"
self.handler?(self.counter)
}
}
}
func configure(with counter: Int) {
self.counter = counter
self.setTimer()
}
private func setTimer() {
self.timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {[weak self] (timer) in
self?.counter += 1
})
}
}
在上面的代码中,
label
,它将更新counter
中的UI
值。handler
-当counter
从屏幕移出时,它将更新的ViewController
值存储在某个地方(在cell
中,进一步说明)timer
-将timer
中的cell
与timeinterval = 1
一起安排counter
-每个counter
的当前cell
值 在ViewController
中,
class VC: UIViewController, UITableViewDataSource {
let numberOfCells = 20
var timerArr = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
self.timerArr = [Int](repeating: 0, count: numberOfCells)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.numberOfCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.configure(with: self.timerArr[indexPath.row])
cell.handler = {[weak self] (counter) in
self?.timerArr[indexPath.row] = counter
}
return cell
}
}
在上面的代码中,
timerArr
-跟踪counter
中每个cell
的{{1}}值。tableView
中,每个tableView(_:cellForRowAt:)
的{{1}}使用我们先前在counter
中创建的cell
进行更新