-2

So I have a UITableView and on top cell(1st cell) I have a timer on the cell.detailText. The timer displays but the tableview can’t scroll past the first cell because the cell text label is constantly being updated by this timer. What can I do to make the table view scroll properly without stopping the timer when the user scrolls? Please help Thanks in advance.

  • 1
    can you please update with question with whatever you done with code. & What do you mean by past cell? – Krunal Nagvadia Mar 30 '19 at 10:33
  • you should not use tableView.reloadData for reloading you should update the specific row with the timer without any animations. Google for reload rows with animation – sanjaykmwt Mar 30 '19 at 11:21
  • check this link out: https://stackoverflow.com/questions/33338726/reload-tableview-section-without-scroll-or-animation – sanjaykmwt Mar 30 '19 at 11:22

1 Answers1

1

You don't need to keep calling reloadData(). You can do all of your timer work in the cell itself - you just need to remember to invalidate timers when you are done with them. I maintain an app that does something similar and use the following:

@IBOutlet weak var timeLeftLbl: UILabel!
var secondTimer: Timer!
var secondsLeft: Int = 0


func configureCell(item: PreviousAuctionItem) {


    //Timers
    secondsLeft = getSecondsLeft(endTime: item.endDate)

    timeLeftLbl.text = secondsLeft

    secondTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.decreaseTimeLeft), userInfo: nil, repeats: true)
}

@objc func decreaseTimeLeft() {
    if secondsLeft > 0 {
        secondsLeft -= 1
        timeLeftLbl.text = secondsLeft.formatTimeAsString()
    } else {
        timeLeftLbl.text = ""
        secondTimer?.invalidate()
    }
}
    override func prepareForReuse() {

    secondTimer?.invalidate()
}

The getSeconds method is an API method I use to get how long an item has left.

Hope this helps!

Iain Coleman
  • 166
  • 1
  • 13