PrepareForReuse() 重置 UIView 宽度不起作用

PrepareForReuse() to reset UIView width not working

我被困在这个问题上有一段时间了,虽然我想我知道问题出在哪里,但我仍然无法解决它。我正在使用 tableView,每个单元格都有倒计时条动画。每个单元格都有一个由用户设置的自定义持续时间。然后随着时间的流逝滑动一个条形动画

我正在使用 CADisplayLink 来获取此动画 运行,此代码是在控制器文件中编写的,我只是随着时间的推移更改 UIView 的宽度:

@objc func handleProgressAnimation() {
    let currenttime = Date()
    let elapsed = currenttime.timeIntervalSince(animationStartDate)
    let totalWidth: Double = 400.0
    print(elapsed)
    let arrayLength = durationBar.count
    var i: Int = 0
    
    
    
    for  _ in 0..<arrayLength {
       let indexPath = IndexPath(row: i, section: 0)
       let percentage = (elapsed / Double(durationBar[i].durationTime))
       let newWidth = Double(totalWidth  - (totalWidth * percentage))
        durationBar[i].width = newWidth
        if (percentage < 1)
        {
            if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? {
                cell.timeRemainingView.frame.size.width = CGFloat(durationBar[indexPath.row].width)
            }
        }
        else {
            if let cell = listTableView.cellForRow(at: indexPath) as! listTableViewCell? {
                cell.timeRemainingView.frame.size.width = 400
                cell.timeRemainingView.isHidden = true
            }
        }
        i = i + 1
    }
 
}

当所有的条都在减少时,一切都很好,但是当其中一个单元格时间完成时,UIView 宽度变为零,然后当用户滚动时,该单元格被重新使用,这会从其他单元格中消失进度条还有时间。

我正在使用 prepareforsegue() 方法将栏的宽度重置为 400(默认值),但这似乎不起作用。

这是cellforRow代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! listTableViewCell
    cell.testlabel.text = "\(duration[indexPath.row].durationTime)"
    cell.timeRemainingView.frame.size.width = CGFloat(durationBar[indexPath.row].width)
    
    return cell
    
}

这是PrepareForReuse()

 override func prepareForReuse() {
    super.prepareForReuse()
    self.timeRemainingView.frame.size.width = 400
    self.timeRemainingView.isHidden = false
}

这是截图。可以看到有的单元格时间到了,有的单元格还有时间但是进度条消失了。

您必须重新设置框架。 width 是一个 get-only 属性.