我有一个表视图列表,它有一个与它绑定的选择操作。 当用户选择该行时,它会给他们一个提示,并开始下载文件并使用渐进式指示器显示进度。 出于某种原因,该指标从选择中每 12 条记录显示一次。 为什么会这样...我是否选择错误的单元格?
这是我的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! downloadCell
let cellName = CurrentFiles[indexPath.row].labelName
let internalName = CurrentFiles[indexPath.row].internalName
let fileLocation = CurrentFiles[indexPath.row].fileName
let safeURL = fileLocation.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
let fileExtension = CurrentFiles[indexPath.row].fileExtension
let fileDate = CurrentFiles[indexPath.row].lastUpdate
if (cell.progressView.isHidden) && (cell.fileStatus == "Installed") && (cell.fileIcon.image == nil) {
let fileLoc = getSharedFilePath(appGroup:applicationGroup,sharedFilename:"(internalName).(fileExtension)")
let defaults = UserDefaults.standard
defaults.set(fileLoc, forKey: "loadmap")
defaults.set(cellName, forKey: "loadmapName")
performSegue(withIdentifier: "gotoMap", sender: self)
} else if (cell.progressView.isHidden) {
alertControllerMsg(msgStyle: UIAlertControllerStyle.alert,msgTitle: "Download File", msgBody: "Are you sure you want to download (cellName)?", cancelLbl: "Cancel", actionLbl: "Download", complete: {
cell.fileStatus = "Installed" //prevent double download
//download file
let destination: (URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions) = {
(temporaryURL, response) in
if let directoryURL = FileManager().containerURL(forSecurityApplicationGroupIdentifier: applicationGroup), let suggestedFilename = response.suggestedFilename {
let filePath = directoryURL.appendingPathComponent("(suggestedFilename)")
return (filePath, [.removePreviousFile, .createIntermediateDirectories])
}
return (temporaryURL, [.removePreviousFile, .createIntermediateDirectories])
}
if self.reachability.isReachableViaWiFi {
cell.progressView.isHidden = false
//BackendAPI is used to allow for downloads in background
BackendAPIManager.sharedInstance.alamoFireManager.download(safeURL!, to: destination)
.downloadProgress { progress in
cell.fileIcon.image = nil
cell.progressView.setProgress(Float(progress.fractionCompleted), animated: true)
cell.pctLabel.text = "(String(format:"%g",round(progress.fractionCompleted*100)))%"
}.response { response in
cell.pctLabel.text = nil
cell.progressView.isHidden = true
cell.additionalLbl.text = nil
UserDefaults(suiteName: applicationGroup)!.set(fileDate, forKey: internalName)
cell.fileStatus = "Installed"
self.getAvailSpace()
}
} else {
self.alertControllerMsg(msgStyle: UIAlertControllerStyle.alert,msgTitle: "Insufficient Network", msgBody: "Please connect to a Wifi network to download this file.", cancelLbl: "Cancel", actionLbl: "Retry", complete: {
self.downloadAndSort()
})
}
})
}
}
编辑:
CellforRowAt 代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "downloadCell", for: indexPath) as? downloadCell {
let currentFile = CurrentFiles[indexPath.row]
cell.configureCell(currentFile: currentFile)
return cell
} else {
return downloadCell()
}
}
编辑2:
class downloadCell: UITableViewCell {
@IBOutlet weak var fileLbl: UILabel!
@IBOutlet weak var fileIcon: UIImageView!
@IBOutlet weak var pctLabel: UILabel!
@IBOutlet weak var additionalLbl: UILabel!
@IBOutlet weak var fileSize: UILabel!
@IBOutlet weak var progressView: UIProgressView!
var fileStatus = "NotInstalled"
func configureCell(currentFile: currentFiles) {
fileLbl.text = currentFile.labelName
fileSize.text = currentFile.fileSize
let internalName = currentFile.internalName
fileIcon.image = UIImage(named: "download")
additionalLbl.text = "Updated: (convertDateFormatter(date: currentFile.lastUpdate))"
let fileExists = (readFromSharedFile(sharedFilename: internalName, fileExtension: currentFile.fileExtension))
if fileExists == "Success" {
//file has been downloaded on device
let lastUpdateDate = UserDefaults(suiteName: applicationGroup)!.string(forKey: internalName)
if lastUpdateDate != currentFile.lastUpdate {
fileIcon.image = UIImage(named: "download")
fileStatus = "NeedsUpdate"
} else {
fileIcon.image = nil
fileStatus = "Installed"
additionalLbl.text = nil
}
} else {
// not on device
fileStatus = "NotInstalled"
}
}
}
在我的视图控制器上,我创建了一个新字典来保存我的文件名和当前进度,默认为 0,并创建了一个数组来保存当前下载的文件
var downloadProg = [String: Double]()
var currentDownloadNames = [String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "downloadCell", for: indexPath) as? downloadCell {
let currentFile = CurrentFiles[indexPath.row]
cell.configureCell(currentFile: currentFile)
if !(currentDownloadNames.contains(currentFile.labelName)) {
cell.progressView.isHidden = true
cell.pctLabel.isHidden = true
cell.fileIcon.isHidden = false
} else {
cell.fileIcon.isHidden = true
cell.progressView.isHidden = false
cell.pctLabel.isHidden = false
}
return cell
} else {
return downloadCell()
}
}
我现在在我的 .downloadProgress 中设置字典百分比
downloadProg[cellName] = progress.fractionCompleted
我修改了下载单元格以显示百分比:
let pct = downloadProg[currentFile.labelName]
if (progressView.isHidden == false){
progressView.setProgress(Float(pct!), animated: true)
pctLabel.text = "(String(format:"%g",round(pct!*100)))%"
}
我们真的需要你的代码来cellForRowAtindex
,并更好地描述问题。屏幕中有多少个单元格(大约是 12 个(?是仅显示每 12 个单元格的进度视图,还是每 12 个单元格中运行实际进度?如果进度视图只是显示但未更新,那么想到的最明显原因是您在dequeueReusableCell(withIdentifier:)
后重新使用单元格时忘记隐藏progressView
cellForRowAtindex
。
在检查器生成器中将 cell.progressview 隐藏标志勾选为 true。 或者您必须打印并检查进度视图的隐藏状态。
听起来您需要在 prepareForReuse 中重置进度视图:
在单元格覆盖中:
override func prepareForReuse() {
}
当单元格被重用时,这会被调用,这听起来像是你自己正在发生的事情。