gpt4 book ai didi

ios - 如何为每个表格 View 单元添加一个计时器,该计时器在用户每次滚动时更新?

转载 作者:行者123 更新时间:2023-11-29 05:41:50 26 4
gpt4 key购买 nike

我正在尝试向每个表格 View 单元格添加一个时间计数器。我启用了分页滚动,表格 View 的每个单元格都是全屏的。

我想要什么:当用户位于某个单元格上时,时间计数器会自动启动,当用户滚动时,时间计数器会停止,并且下一个单元格的另一个计数器会启动...等等

我还想记录一下流逝的时间。

我尝试过的:要向 View Controller 而不是 TableView 添加计时器,我有两个问题:

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

我希望为表格 View 的每个单元格触发一个计时器,并且我希望将耗时保存在一个可以在其他地方使用的变量中

最佳答案

以下是如何处理每个 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
})
}
}

在上面的代码中,

  1. 我创建了一个标签,它将更新UI中的计数器值。
  2. handler - 当 cell 时,它会将更新的 counter 值存储在某处(在 ViewController 中,进一步解释) code> 被移出屏幕
  3. timer - 使用 timeinterval = 1 安排 cell 中的timer
  4. counter - 每个单元格的当前counter

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

在上面的代码中,

  1. timerArr - 跟踪 tableView 中每个单元格counter值。
  2. tableView(_:cellForRowAt:) 中,每个 cellcounter 使用 handler 进行更新我们之前在 CustomCell
  3. 中创建

关于ios - 如何为每个表格 View 单元添加一个计时器,该计时器在用户每次滚动时更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56480771/

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