简体   繁体   中英

how to add a timer for each tableview cell that updates everytime a user scrolls?

I'm trying to add a time counter to each of my tableview cells. I have enabled scrolling with pagination, each cell of my tableview is full screen.

What I want: When a user is on a cell, the time counter starts automatically and when they scroll, the time counter stops, and another one for the following cell starts...etc

I also want to keep a record of the time elapsed.

what I tried: to add a timer to the view controller instead of tableview, I have 2 problems:

1) The timer is not reset when I swipe up although I invalidate it as you can see below

2) The timer starts before the first cell has loaded its content from the server

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

I want a timer to fire for each cell of my tableview, and i want to keep the elapsed time in a variable that I can use elsewhere

Here is how you can handle timers in each UITableViewCell .

Create a custom 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
        })
    }
}

In the above code,

  1. I've created a label that will update the counter value in UI .
  2. handler - it will and store the updated counter value somewhere (in ViewController , explained further) when the cell is moved out of the screen
  3. timer - schedule the timer in the cell with timeinterval = 1
  4. counter - current counter value for each cell

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

In the above code,

  1. timerArr - keeps track of the counter value for each cell in the tableView .
  2. In tableView(_:cellForRowAt:) , the counter for each cell is updated using the handler we created previously in CustomCell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM