UIProgressView正在UITableViewCell中重置



在我的tableviewCell类中,我将progressview设置为(忽略类名,使用Food作为简单性(:

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

然后在我的UITableView中,我称之为:

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
}

然而,进度视图会自动更新,0.5秒后,我看到它被重置了。有人对我们如何解决这个问题有什么想法或建议吗?

我找到了解决方案,因为它只是一个小错误。Int显然不允许在Swift中分割,因此您需要转换为Double,并重新转换为Float,以便在ProgressView中使用,如下所示:

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

干杯,希望这能帮助

最新更新