首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PrepareForReuse()重置UIView宽度不起作用

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

Stack Overflow用户
提问于 2018-07-30 21:12:26
回答 1查看 926关注 0票数 1

我现在被困在这个问题上有一段时间了,虽然我想我知道问题是什么,但我仍然无法解决它。我和tableView一起工作,每个单元都有倒计时条动画。每个单元格都有用户设置的自定义持续时间。然后,条状动画随着时间的推移而滑动。

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

代码语言:javascript
复制
@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代码:

代码语言:javascript
复制
 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()

代码语言:javascript
复制
 override func prepareForReuse() {
    super.prepareForReuse()
    self.timeRemainingView.frame.size.width = 400
    self.timeRemainingView.isHidden = false
}

这是截图。你可以在一些细胞中看到时间已经过去,但在另一些细胞中,还有剩余的时间,但是进度条已经消失了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-31 17:26:21

你得重新设置一下框架。width是一个纯获取属性.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51602360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档