gpt4 book ai didi

ios - 为什么 UITableViewCell 在滚动可用单元格后显示错误数据?

转载 作者:行者123 更新时间:2023-11-28 10:18:54 27 4
gpt4 key购买 nike

我正在为 iOS 制作一个下载管理器,但问题是当我滚动屏幕后单元格多于它们可以容纳屏幕时,它开始为示例提供错误数据:

  • 在表格 View 开始时完成/暂停/正在进行的下载滚动后它可能会显示在第一行或最后一行

代码如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("downloadCell", forIndexPath: indexPath) as! RCHDownloadAddictTableViewCell

updateCell(cell, forRowAt: indexPath)

return cell
}

func updateCell(cell: RCHDownloadAddictTableViewCell, forRowAt indexPath: NSIndexPath) {

let arrayInfo = downloadingArray[indexPath.row]

cell.cellProgressView.setProgress((arrayInfo.downloadProgress)!, animated: true)
cell.cellFileName.text = arrayInfo.fileName
cell.cellDownloadSpeed.text = String(format: "%.1fMB", (arrayInfo.downloadSize / 1000000))
cell.cellBlock = {

if (arrayInfo.downloadTask.state == NSURLSessionTaskState.Running) {
arrayInfo.downloadTask.suspend()
cell.cellButton.setImage(UIImage(named: "resume.png"), forState: UIControlState.Normal)
} else if (arrayInfo.downloadTask.state == NSURLSessionTaskState.Suspended) {
arrayInfo.downloadTask.suspend()
cell.cellButton.setImage(UIImage(named: "resume.png"), forState: UIControlState.Normal)
}

}

if (arrayInfo.didFinishDownload == true) {
cell.cellButton.hidden = true
cell.cellFinishIndicator.text = "Finished."
cell.cellProgressView.hidden = true
cell.cellFinishIndicator.hidden = false
} else {
cell.cellButton.hidden = false
cell.cellProgressView.hidden = false
cell.cellFinishIndicator.hidden = true
}
}

更新:观看此视频并关注第一个单元格 https://www.youtube.com/watch?v=S_PnAS5lb7s&feature=youtu.be

最佳答案

表格 View 单元格通过重用队列重用。如果您一次在屏幕上看到 7 个单元格,当您向下滚动(更多手指向上)时,移出顶部边缘边界的单元格将从重用队列传递到底部再次边缘。该单元格是一个初始化实例,此外您还为该单元格实例的公共(public)属性设置了一些具体值。它是一个视觉上消失但仍在内存中然后重新出现的对象。您需要做的是在它消失和重新出现之间清理它。

正确的位置和方法是这样的:

1) 视觉效果,即与 UIView 属性相关的东西,如 .hidden.alpha.backgroundColor 或选择状态应重置为所需prepareForReuse 中的默认状态自定义单元格中的覆盖方法。不要忘记调用 super。

2) 对于内容相关的东西,比如下载进度,我建议在单元格上实现一个custom public reset method,在方法内部做无论你想要什么,不属于 prepareForReuse 组,并在你从队列中取出单元格后立即在 cellForRowAtIndexPath 中调用这个重置方法。

关于ios - 为什么 UITableViewCell 在滚动可用单元格后显示错误数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37361048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com