Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift TableView ProgressView指示器在单选屏幕上显示多行_Swift_Tableview - Fatal编程技术网

Swift TableView ProgressView指示器在单选屏幕上显示多行

Swift TableView ProgressView指示器在单选屏幕上显示多行,swift,tableview,Swift,Tableview,我有一个tableview列表,其中绑定了一个select操作。当用户选择行时,它会给他们一个提示,并开始下载文件,并使用渐进指示器显示进度。由于某些原因,该指示器每12条记录显示一次。为什么会这样。。。我是否选择的单元格不正确 这是我的密码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = self.tableView.cellForRow(at: in

我有一个tableview列表,其中绑定了一个select操作。当用户选择行时,它会给他们一个提示,并开始下载文件,并使用渐进指示器显示进度。由于某些原因,该指示器每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"

    }
}
}

我们还需要您的
cellforrowatinex
代码以及对问题的更好描述。屏幕上有多少个单元格(大约12个)?每12个单元格只显示一个进度视图,还是每12个单元格都显示一个实际进度?如果只显示了进度视图,但没有更新,那么最明显的原因是您忘记了在
cellForRowAtindex
中隐藏
progressView
,因为该单元格在
出列后重新使用​可重复使用的​单元(带​标识符:​)

在inspector builder中将cell.progressview hidden标志勾选为true。或者您必须打印并检查progressview的隐藏状态。

听起来您需要在prepareForReuse中重置进度视图:

在单元格覆盖中:

override func prepareForReuse() {

}

当单元格被重新使用时,就会调用它,这听起来像是你自己在做的事情。

在我的视图控制器上,我创建了一个新的字典来保存默认为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)))%"
    } 

不相关,但为什么强制展开单元格(即使有两个
),但始终使用可选链接(
单元格?…
)?谢谢,我一直想把它清理干净。我想这是因为仅仅依靠自动更正的结果。但这对我的问题没有影响。根据Vadian编辑以清理代码使用
cellforrowatinex
方法更新您的问题以检查您如何创建单元格。我认为您的单元格正在被重用这里,与您的问题非常相似。他使用一个进度数组来跟踪下载的进度,并为单元格上的进度视图设置正确的值。检查器中cell.progressview的隐藏标志已选中Serggr我已使用cellForRowatinex代码编辑了帖子。我的列表v视图可能有大约30个值,开头显示了11个值(横向视图)。当我单击第三行时,它还会对该行下面的13行运行进度更新。它似乎也在加载该文件,但我已确认它只下载了我选择的一行。@Nate23VT,显然提供这样的
cellforrowatinex
而不调用任何方法是没有意义的。
conf如果文件未下载,我将退出
hide
progressView
?如果不使用
dequeue怎么办​可重复使用的​Cell
每次都创建一个新的Cell?为什么
downloadCell
call没有将
currentFile
作为参数接收?我已经在编辑中添加了downloadCell类代码-我正在考虑根据您的问题进行返工,但我是SWIFT的新手看起来我无法将奖金奖励给自己,这个答案让我获得了最大的成功n track.Nate,很抱歉,我一直很忙,没有跟进您的澄清请求。虽然您当前的代码更好,但在我前面指出的方面,我仍然觉得奇怪:
return downloadCell()
我觉得没有任何配置的分支非常可疑。如果正在下载,当用户旋转设备时,您可能会遇到一些问题。