如何在customcell中更新进度视图

时间:2016-01-25 21:36:42

标签: ios swift uitableview

在我的应用中,我有自定义单元格的uitableview。 每个单元格都有一个按钮来选择它,并将自定义的nsmanagedobject添加到数组中。 然后我迭代这个数组来下载所选的文件。

正在下载工作,但我仍然坚持在单元格进度视图中显示进度,尤其是当用户在下载过程中滚动tableview时。

这是我的cellForRowAtIndexPath

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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CuctomCell

    let file = fileArray[indexPath.row] as! File
    cell.titleLabel.text = file.name

    if (file.isDownloaded != 0)
    {
        //File is downloaded and can be read
        cell.readLabel.hidden = false
        cell.readLabel.text = "READ"
        cell.selectBtn.hidden = true
        cell.fileLabel.hidden = true
        cell.progressView.hidden = true
        cell.fileLabel.text = file.filePath
        cell.fileLabel.hidden = false
        cell.openButton.hidden = false
    }
    else
    {
        //File is not downloaded show the select button to download
        cell.readLabel.hidden = true
    }

    if (self.selectedButtonsArray.containsObject(indexPath.row))
    {
        cell.selectBtn.selected = true
    }
    else
    {
        cell.selectBtn.selected = false
    }

    cell.tag = indexPath.row
    cell.selectBtn.tag = indexPath.row
    cell.progressView.tag = indexPath.row
    cell.selectBtn.addTarget(self, action: "downLoadFiles:", forControlEvents: .TouchUpInside)
    cell.openButton.tag = indexPath.row
    cell.openButton.addTarget(self, action: "openPDF:", forControlEvents: .TouchUpInside)

    return cell
}

将所选单元格添加到下载数组的操作

@IBAction func downLoadFiles(sender: UIButton)
{
    let updateFile = self.fileArray.objectAtIndex(sender.tag) as! File

    if (downloadArray.containsObject(updateFile))
    {
        updateObjectIdentifierTag(updateFile, tag: sender.tag, action: "remove")
        downloadArray.removeObject(updateFile)
        selectedButtonsArray.removeObject(sender.tag)

        //print(downloadArray)
        sender.selected = false
    }
    else
    {
        updateObjectIdentifierTag(updateFile, tag: sender.tag, action: "add")
        downloadArray.addObject(updateFile)
        selectedButtonsArray.addObject(sender.tag)
        sender.selected = true
    }

    let addString = "ADD (\(downloadArray.count))"

    addButton = UIBarButtonItem(title: addString, style: UIBarButtonItemStyle.Done, target: self, action: "updateCoreData")

    self.navigationItem.leftBarButtonItem = addButton

}

func updateObjectIdentifierTag(mObject: File , tag: Int, action: String)
{
    let object = self.fileArray.objectAtIndex(tag) as! File

    if (action == "add")
    {
        object.tagIndex = tag
    }
    else if (action == "remove")
    {
        object.tagIndex = -1
        object.taskIdentifier = -1
    }

    do
    {
        try context.save()
    }
    catch
    {
        print("Unable to save")
    }

}

我为下载创建了一个nsdictionary数组,为indexPath添加了一个键。

试过这段代码

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

    let predicate = NSPredicate(format:"ident == %d", downloadTask.taskIdentifier)
    let arr = self.selestedIndexPaths.filteredArrayUsingPredicate(predicate)


    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        let indexpath = arr[0]["indexPath"] as! NSIndexPath
        let cell = self.tableView.cellForRowAtIndexPath(indexpath) as! CuctomCell
        cell.progressView.hidden = false
        let progress = Float(totalBytesWritten / totalBytesExpectedToWrite)
        cell.progressView.progress = progress
        print("IndexPath: \(indexpath)")

        })
}

但是我得到“致命错误:当单元格不可见时,在展开”可选值时意外地发现nil“。

请提出任何建议。

0 个答案:

没有答案