如何为每个用户每次滚动更新的表视图单元格添加一个计时器?

时间:2019-06-06 15:28:55

标签: ios swift uitableview timer

我正在尝试为每个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)
}

我希望为表视图的每个单元格触发一个计时器,并且我希望将经过的时间保留在可以在其他地方使用的变量中

1 个答案:

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

在上面的代码中,

  1. 我创建了一个label,它将更新counter中的UI值。
  2. handler-当counter从屏幕移出时,它将更新的ViewController值存储在某个地方(在cell中,进一步说明)
  3. timer-将timer中的celltimeinterval = 1一起安排
  4. 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
    }
}

在上面的代码中,

  1. timerArr-跟踪counter中每个cell的{​​{1}}值。
  2. tableView中,每个tableView(_:cellForRowAt:)的{​​{1}}使用我们先前在counter中创建的cell进行更新