1

In my tableviewCell class, i set the progressview as (ignore the class name, using Food as simplicity) :

func setupCell(list: Food) {
    documentTitle.text = list.nickname
    if let spoiltFood = list.spoilt, let totalFood = list.totalFood {
        DispatchQueue.main.async {
            self.progressView.progress = Float(spoiltFood/totalFood)
        }
    }
}

Then in my UITableView, i call it as :

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! FoodTableViewCell
    if let foodList = self.foodList {
        cell.setupCell(list: foodList[indexPath.row])
    }
    return cell
}

However, the progress view updates itself and right after 0.5 seconds, i see it being reset. Does anyone have any ideas or suggestion on how we are able to solve the issue?

L.William
  • 382
  • 1
  • 4
  • 20
  • Typically you'd put a progress update inside of a timer so that you can update at an interval, then set a flag to `.invalidate()` that timer when your process is done. That will result in a much smoother progress update. – xTwisteDx Apr 15 '21 at 14:55
  • @xTwisteDx I am using progressview due to the UI of it. Is timer mandatory for it to work? – L.William Apr 15 '21 at 15:00

1 Answers1

0

I found the solution whereby it just due to a small mistake. Int aren't allowed to divide in Swift apparently, therefore you need to convert to Double, and re-convert to Float, to be used in ProgressView as below:

func setupCell(list: Documents) {
    if let spoiltFood = list.spoiltFood, let totalFood = list.totalFood {
        DispatchQueue.main.async {
            let fraction = Double(spoiltFood)/Double(totalFood)
            self.progressView.progress = Float(fraction)
        }
    }
}

Cheers all, hope this may help

L.William
  • 382
  • 1
  • 4
  • 20