【问题标题】:PrepareForReuse() to reset UIView width not workingPrepareForReuse() 重置 UIView 宽度不起作用
【发布时间】:2019-01-07 04:47:24
【问题描述】:

我现在被困了一段时间,虽然我想我知道是什么问题,但我仍然无法解决它。我正在使用 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。你必须重新设置整个frame。
  • 搞定了!谢谢

标签: ios swift uitableview reusability


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 2014-11-01
    • 2023-03-29
    相关资源
    最近更新 更多