为什么以这种方式更新UITableView的单元格是错误的?

时间:2016-05-24 10:55:42

标签: ios swift uitableview

我正在制作一个下载管理器,由于“可重复使用的单元格”,我一直在努力更新单元格....

我在滚动后更新单元格时出现问题,但是我发现了一种无法正常工作的解决方法,这种方式导致应用程序在单元格变得不可见后崩溃,我不知道为什么我希望你们能够解释一下,如果你知道如何解决它,请告诉我。

我以这种方式添加下载任务

func addDownloadTask(URL: NSURL) {

    let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    let mainQueue = NSOperationQueue.mainQueue()
    let session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: mainQueue).downloadTaskWithURL(URL)

    let newDownload = RCHDownloadAddictModelClass(fileURL: URL)

    newDownload.downloadTask = session

    downloadingArray.append(newDownload)

    newDownload.downloadIndex = downloadingArray.indexOf(downloadingArray.last!)

    self.tableView.reloadData()

    session.resume()

}

NSURLSessionDownloadTask开始下载此方法时,会调用

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

    for (index, downloadModel) in downloadingArray.enumerate() {
        if downloadTask.isEqual(downloadModel.downloadTask) {
            dispatch_async(dispatch_get_main_queue(), {

                let indexPath = NSIndexPath(forRow: index, inSection: 0)
                /* xCode stops here when the app crashes */ let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! RCHDownloadAddictTableViewCell 
                let countOfBytesWritten: Double!
                if totalBytesExpectedToWrite < 0 {

                    countOfBytesWritten = 0

                } else {

                    countOfBytesWritten  = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)

                }

                downloadModel.fileName = downloadTask.response?.suggestedFilename
                downloadModel.downloadIndex = downloadingArray.indexOf(downloadModel)
                downloadModel.downloadSize = Double(totalBytesExpectedToWrite)
                downloadModel.downloadProgress = Float(countOfBytesWritten)
                downloadModel.downloadTaskIdentifier = downloadTask.taskIdentifier
                downloadModel.didFinishDownload = false

                self.updateCell(cell, forRowAt: indexPath)

            })
        }
    }


}

这是单元格更新的方式:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("downloadCell", forIndexPath: indexPath) as! RCHDownloadAddictTableViewCell

    updateCell(cell, forRowAt: indexPath)

    return cell
}



func updateCell(cell: RCHDownloadAddictTableViewCell, forRowAt indexPath: NSIndexPath) {

    let arrayInfo = downloadingArray[indexPath.row]

    cell.cellProgressView.setProgress((arrayInfo.downloadProgress)!, animated: true)
    cell.cellFileName.text = arrayInfo.fileName
    cell.cellDownloadSpeed.text = String(format: "%.1fMB", (arrayInfo.downloadSize / 1000000))
    cell.cellBlock = {

        switch arrayInfo.downloadTask.state {
        case .Running:
            arrayInfo.downloadTask.suspend()
            cell.cellButton.setImage(UIImage(named: "resume.png"), forState: UIControlState.Normal)
        case .Suspended:
            arrayInfo.downloadTask.resume()
            cell.cellButton.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal)
        default:
            arrayInfo.downloadTask.suspend()
            cell.cellButton.setImage(UIImage(named: "resume.png"), forState: UIControlState.Normal)
        }

    }

    if (arrayInfo.didFinishDownload == true) {
        cell.cellButton.hidden = true
        cell.cellFinishIndicator.text = "Finished."
        cell.cellProgressView.hidden = true
        cell.cellFinishIndicator.hidden = false
    } else {
        cell.cellButton.hidden = false
        cell.cellProgressView.hidden = false
        cell.cellFinishIndicator.hidden = true
    }

}

2 个答案:

答案 0 :(得分:0)

你可以在apple doc上看到它:

public func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell? 
// returns nil if cell is not visible or index path is out of range

//cell will be nil when this row is not visible
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! RCHDownloadAddictTableViewCell 

然后你打电话给self.updateCell(cell, forRowAt: indexPath)它会崩溃

答案 1 :(得分:0)

您使用代码的路径正确,但有一个例外。

下载完成后,您已将正确的数据存储到正确位置的数组中,您应该运行代码...

tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:.Automatic)

然后,这将触发表以正确的索引路径重新加载单元格。如果该单元格在屏幕上,它将重新加载。如果该单元格不在屏幕上,则不会重新加载。但是下次显示单元格时,它将显示正确的信息。

实质上。不要直接更新单元格。只需告诉tableview您已更改该特定行的数据。