gpt4 book ai didi

单选多行上的 Swift TableView ProgressView 指示器

转载 作者:可可西里 更新时间:2023-11-01 02:05:52 25 4
gpt4 key购买 nike

我有一个 TableView 列表,其中有一个选择操作与之相关。当用户选择该行时,它会提示他们并开始下载文件并使用进度指示器显示进度。出于某种原因,指标从选择中每 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"

}
}
}

最佳答案

在我的 View Controller 上,我创建了一个新字典来保存我的文件名和默认为 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

我修改了 downloadCell 以显示百分比:

let pct = downloadProg[currentFile.labelName]
if (progressView.isHidden == false){
progressView.setProgress(Float(pct!), animated: true)
pctLabel.text = "\(String(format:"%g",round(pct!*100)))%"
}

关于单选多行上的 Swift TableView ProgressView 指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42910471/

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