2016-03-17 53 views
1

我有一個使用UICollectionView設計的聊天視圖。我已經成功實現了圖片上傳功能,只要用戶使用圖片選擇器點擊圖片,圖片就會上傳到服務器。現在我正在試圖爲每張圖片添加進度條來顯示上傳狀態。目前我所做的是,無論何時用戶使用UIImagePicker選擇一個圖像,我的單元格將獲得最新圖像的更新,而集合視圖會滾動到底部,並在URLsession委託中捕獲最後一個單元格並顯示進度,但問題是,當我在上傳過程中選擇另一個圖像時,兩個進度條顯示在最後一個單元格上。下面是我對圖像拾取和會議代表代碼在swift中顯示UICollectionView中的多個圖像上傳進度條

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

     let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

     selectedImage = chosenImage 

     //Some other code to update cell data 

picker.dismissViewControllerAnimated(true, completion: {() -> Void in 
      self.scrollToBottomAnimated(true) 

     }) 

let qualityOfServiceClass = QOS_CLASS_BACKGROUND 
     let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0) 
     dispatch_async(backgroundQueue, { 
      print("To run image upload in background") 

      self.uploadImage(UIImageJPEGRepresentation(self.selectedImage!, 0.5)!, parameters: self.createDictionaryForUploadImageDetails()) { (responseDict, error) ->() in 

       if (responseDict != nil) { 
        print(responseDict!) 
       } 
       else 
       { 
        if error != nil 
        { 
         Utilities.showAlertViewMessageAndTitle((error?.localizedDescription)!, title: "Error", delegate: [], cancelButtonTitle: "OK") 
        } 

       } 

      } 
     }) 
} 

NSURLSession代表

func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) 
    { 
     let uploadProgress:Float = Float(totalBytesSent)/Float(totalBytesExpectedToSend) 

     let section = self.numberOfSectionsInCollectionView(self.chatCollectionView) - 1 
     let item = self.collectionView(self.chatCollectionView, numberOfItemsInSection:section)-1 
     let finalIndexPath = NSIndexPath(forItem: item, inSection: section) 

     if let selectedCell = chatCollectionView.cellForItemAtIndexPath(finalIndexPath) 
     { 

      selectedCell.progressView.hidden = false 
      selectedCell.progressView = Int(uploadProgress * 360) 
      let progressPercent = Int(uploadProgress*100) 
      selectedCell.progressView = Int(uploadProgress) == 1 
      print("Progress-\(progressPercent)% - \(progressView.hidden)") 

     } 
    } 

我怎樣才能得到每個上傳正確的電池?

回答

1

你缺少的是數據模型,所以你目前的問題不會是你唯一的問題。

您的數據模型應該像自定義類的數組一樣,其中自定義類具有要顯示的圖像的存儲空間,還包括有關上載過程的信息,正在處理它的任務,百分比完成,...

現在,一旦這個數據模型就緒,任務委託就可以很簡單地在數組中找到正確的實例來更新進度細節。數組中的索引告訴你需要更新UI的單元格。當你滾動單元格時(所以當新的單元格實例被請求創建/更新時),你仍然可以顯示所有的進度信息。沒問題。

+1

沒錯。換句話說,當細胞即將被顯示時,它將顯示模型(圖像)的當前狀態,例如,如果正在下載,您將顯示當前百分比,單元格將開始監聽模型更改(委託模式)。在每個時刻,由於單個單元格映射到單個模型對象,因此只能有一個模型委託集。 – Mercurial

相關問題