在我的 tableviewCell 类中,我将进度视图设置为(忽略类名,使用 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 秒后自动更新,我看到它被重置了。有没有人对我们如何解决问题有任何想法或建议?
答案 0 :(得分:0)
我找到了解决方案,它只是由于一个小错误。 Swift 中显然不允许 Int 进行除法,因此您需要转换为 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)
}
}
}
干杯,希望这可以帮助