Ios 如何在customcell中更新进度视图
在我的应用程序中,我有一个自定义单元格的uitableview。 每个单元格都有一个按钮,用于选择该单元格并将自定义nsmanagedobject添加到数组中。 然后,我迭代该数组以下载所选文件 下载工作正常,但我一直坚持在cell progressview上显示进度,尤其是当用户在下载过程中滚动tableview时 这是我的手机号码Ios 如何在customcell中更新进度视图,ios,swift,uitableview,Ios,Swift,Uitableview,在我的应用程序中,我有一个自定义单元格的uitableview。 每个单元格都有一个按钮,用于选择该单元格并将自定义nsmanagedobject添加到数组中。 然后,我迭代该数组以下载所选文件 下载工作正常,但我一直坚持在cell progressview上显示进度,尤其是当用户在下载过程中滚动tableview时 这是我的手机号码 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPa
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”
有什么建议吗。有什么问题吗?你说你被卡住了,但你没有提到什么。@rocky我被困在函数中:didwritedata,我应该在每个正在下载的单元格中显示进度视图,我如何在该函数中实现逻辑以显示当前正在下载的单元格,即使该单元格不可见?