UIProgressView is not updating inside UICollectionView in Swift3 iOS

こ雲淡風輕ζ 提交于 2019-12-11 18:51:19

问题


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!

来源:https://stackoverflow.com/questions/51359471/uiprogressview-is-not-updating-inside-uicollectionview-in-swift3-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!