Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.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

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
Ios UITableView更新错误的单元格_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView更新错误的单元格

Ios UITableView更新错误的单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个具有同一类的多个单元格的Tableview。在单元内更改开关时,应下载特定文件 实际行为: 正确的开关正在改变其状态,即使在滚动时也会存储,因此这不是问题所在。开始下载时CountryDesigner是正确的,完成下载时也是正确的文件,但是存储文件时,它使用了错误的CountryDesigner,progressBar也是错误的。在这种特殊情况下,点击Canada将导致下载文件[…]ca_asp.aip,并且Canada单元格中的开关已更改,但单元格的progressBar正在移动,存储

我有一个具有同一类的多个单元格的Tableview。在单元内更改开关时,应下载特定文件

实际行为: 正确的开关正在改变其状态,即使在滚动时也会存储,因此这不是问题所在。开始下载时CountryDesigner是正确的,完成下载时也是正确的文件,但是存储文件时,它使用了错误的CountryDesigner,progressBar也是错误的。在这种特殊情况下,点击Canada将导致下载文件[…]ca_asp.aip,并且Canada单元格中的开关已更改,但单元格的progressBar正在移动,存储的文件在_asp_aip处调用

代码:


这是一个常见的问题。因为
UITableViewCell
是可重用的
单元,所以您可以保存该单元,但滚动时该单元将显示不同的
数据源

您不应该保存单元格,而是根据数据源记录您正在下载的内容

    func startDownloading () {
        guard let indexPath = self.indexPath else { return }
        countrySettings[indexPath.row].isEnabled = countrySwitch.isOn
        countrySettings[indexPath.row].isDownloading = true
        DispatchQueue.main.async {
            print(self.countryDesignator)  // prints correct Designator
            downloadCount += 1
            if downloadCount == 1 {
                let url = URL(string: "https://www.blablabla.com/" + self.countryDesignator + "_asp.aip")!
                countrySettings[indexPath.row].downloadTask = self.defaultSession.downloadTask(with: url)
                countrySettings[indexPath.row].downloadTask.resume()
            }
        }
    }
并始终更改向哪个单元格显示此下载数据源的
countryDesignator

    func startDownloading () {
        guard let indexPath = self.indexPath else { return }
        countrySettings[indexPath.row].isEnabled = countrySwitch.isOn
        countrySettings[indexPath.row].isDownloading = true
        DispatchQueue.main.async {
            print(self.countryDesignator)  // prints correct Designator
            downloadCount += 1
            if downloadCount == 1 {
                let url = URL(string: "https://www.blablabla.com/" + self.countryDesignator + "_asp.aip")!
                countrySettings[indexPath.row].downloadTask = self.defaultSession.downloadTask(with: url)
                countrySettings[indexPath.row].downloadTask.resume()
            }
        }
    }