在Swift3 iOS中,UIProgressView不会在UICollectionView内部更新

时间:2018-07-16 10:14:39

标签: ios swift3 uicollectionview uiprogressview

我是Swift3的新手。我正在从我的应用程序将视频上​​传到Amazon S3服务器,并且为每个视频设置了一个进度视图。当我添加视频时,我的ProgressView正在更新,但是我的问题是当我从屏幕上返回并再次出现时,ProgressView没有更新。我使用断点进行检查,然后调用了集合视图的cellForItemAt方法,但进度视图未更新。我已使用“自定义”单元格添加了UIProgressView。

这是我的代码:

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videocell", for: indexPath) as! VideoCell

        let videos = videoUrlsArr.object(at: indexPath.row) as! VideoModel

        if videos.uploadProgress == 1.0{
            cell.progressView.isHidden = true
        }
        else{
            cell.progressView.isHidden = false
            cell.progressView.setProgress(videos.uploadProgress, animated: true)
        }

        return cell
    }

//将视频文件上传到S3

func uploadMP4MediatoAmazonS3(videos: VideoModel, data:Data) {

        let expression = AWSS3TransferUtilityUploadExpression()

        expression.progressBlock = { (task: AWSS3TransferUtilityTask, progress: Progress) in
            print(progress.fractionCompleted)
            DispatchQueue.main.async(execute: {

                self.progress = Float(progress.fractionCompleted)
                videos.uploadProgress = self.progress

                self.collectionView.reloadData()
                self.collectionView.collectionViewLayout.invalidateLayout()
                self.collectionView.layoutSubviews()

                print("Progress: \(self.progress)")
            })
        }//progress expression....

        self.uploadCompletionHandler = { (task, error) -> Void in
            DispatchQueue.main.async(execute: {
                if ((error) != nil){
                    print("Failed with error")
                    print("Error: %@",error!);
                }
                else if(self.progress != 1.0) {
                    print("Error: Failed - Likely due to invalid region / filename")
                }
                else{

                    videos.uploadProgress = 1
                    print("Success")
                }
            })
        }//completion handler for upload....


        let transferUtility = AWSS3TransferUtility.default()

        transferUtility.uploadData(data, bucket: S3BucketName, key: videos.videoPath, contentType: "", expression: expression, completionHander: uploadCompletionHandler).continue ({ (task:AWSTask) -> Any? in
            if let error = task.error {
                print("Error: %@",error.localizedDescription);
            }
            if let exception = task.exception {
                print("Exception: %@",exception.description);
            }
            if let _ = task.result {
                print("Upload Started...")
            }

            return nil;
        })

    }

//我的自定义单元格类

import UIKit

class VideoCell: UICollectionViewCell {
    @IBOutlet weak var videoImage: UIImageView!

    @IBOutlet weak var playIcon: UIImageView!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var wbView: UIWebView!

    @IBOutlet weak var progressView: UIProgressView!
}

请建议我。谢谢!

0 个答案:

没有答案