UIProgressView is not updating inside UICollection

2019-08-28 01:26发布

问题:

I am new to Swift3. I am uploading Video to Amazon S3 server from my Application and for each Video I have set one progress view. When I am adding a video that time my ProgressView is updating but my problem is when I am going back from the screen and come again then ProgressView is not updating. I checked using breakpoint then my Collection View cellForItemAt method is invoking, but progress view is not updating. I have added UIProgressView using Custom cell.

Here is my code:

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
    }

//Upload Video file to 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;
        })

    }

//My custom cell class

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!
}

Please suggest me. Thank you!