在UITableView
内,我有一个带下载按钮的自定义单元格。点击下载按钮后,我用indexPath.row
更新标签,并在下载功能进度视图中显示该单元格。问题是当用户滚动并且单元格变得不可见时,特定进度视图开始显示在不同的单元格中。
这是我的cellForRowAtIndexPath
功能:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:CellWithDownload! = tableView.dequeueReusableCellWithIdentifier("CellWithDownload") as CellWithDownload
var title = rowDataAll[indexPath.row].valueForKey("BayanTitle") as String
cell.TitleLabel.text = title
cell.AuthorLabel.text = rowDataAll[indexPath.row].valueForKey("Artist") as? String
var countOfLikesString =
cell.downloadButton.tag = indexPath.row
cell.downloadButton.addTarget(self, action: Selector("downloadAudio:"), forControlEvents: UIControlEvents.TouchUpInside)
// If cell becomes visible again, then star
if let isDownloading = rowDataAll[indexPath.row].valueForKey("isDownloading") {
if (isDownloading as NSString == "true") {
cell.showDownloading()
cell.progressView.progress = getDownloadObjectWithURL(url).progress
} else if (isDownloading as NSString == "completed") {
cell.hideDownloading()
}
}
return cell
}
请建议。
答案 0 :(得分:1)
最有可能的问题是,重复使用后,您不会重新初始化单元格。当您将新单元格出列时,它可能会重新使用其中一个刚刚滚出屏幕的单元格。如果显示进度条并且您从未将出列单元格初始化为没有进度条,则只要重新使用具有进度条的单元格,它就会出现。
所以我认为你需要确保在出列单元后初始化单元格。
在你的cellForRowAtIndexPath中我认为你需要添加一个else语句来处理一个没有下载的单元格并且没有设置进度条:
if let isDownloading = rowDataAll[indexPath.row].valueForKey("isDownloading"){
<Your existing code seems to setup the progress bar here>
}
else{
<Set it to have no pogress bar in here.>
cell.hideDownloading()
}