2016-09-16 107 views
2

我有UITableView與自定義單元格。我想顯示多個圖片上傳的進度指示器。顯示UITableViewCell中多個圖像上傳的進度指示器

我試過reloadRowAtIndexPath方法的UITableView,但它不是足夠的解決方案,因爲細胞不斷閃爍,這看起來很奇怪。

另一種解決方案,我發現是放置在的UITableViewCell在全局變量然後修改它外面的UITableView數據源的方法我的進度指示器視圖的存儲參考,但在該溶液中我面臨一個問題,就是我必須保持跟蹤UITableViewCell的多個進度指示器視圖對象,這很難,因爲UITableView數據源是二維的NSMutableArray(數組中的簡短數組),所以我沒有獨特的IndexPath.row因爲有多個節。那麼我如何管理進度指標視圖的對象呢?

還有沒有更好的解決方案來做這種工作?

+0

多張圖片上傳,什麼ü通過它的意思是,你下載或上傳圖片到服務器 – ChenSmile

+0

我正在使用API​​將圖像上傳到服務器。 –

+0

您是否使用AFNetworking上傳圖片? –

回答

0

這裏我應用的解決方案可能對需要相同實現的人有所幫助,而不需要使用第三方庫或類。

我已經創建了一個自定義UIView並使用CALayer和一些動畫設計循環進度指示器。這不是什麼大不了的事。但是對我來說很難的事情是我想要在幾個單元格中顯示多個圖像進度百分比的進度指示器。

所以我已經創建了一個屬性自定義類像

@property (nonatomic,retain) NSIndexPath    *indexPath; 
@property (nonatomic,strong) NSURLSessionTask   *uploadtask; 

然後,我保持一個NSMutableArray其中含有對每個uploadTask值目前上傳圖片我的自定義類對象和合幷包含indexPath字符串。現在我已經跟蹤了我的所有上傳圖片,所以我通過索引路徑值幫助我更改了自定義進度指示器UIView中的上傳百分比,只要我收到NSURLSession代表方法的響應。

+0

你好@Dhaval Dobariya你能幫我解決這個問題嗎https://stackoverflow.com/q/46047271/2910061? – ilesh

1

好吧,所以這裏是我在其中一個項目中使用的東西,當我找不到其他東西時。

斯威夫特3

使子類的NSObject的(因爲一個子類URLSession不會讓你設置的配置和其他參數作爲唯一指定初始化有的init()),它包含的信息像在IndexPath中一樣啓動上載過程的單元以及一個URLSession對象。

每當你想上傳時(使用URLSession的uploadTask方法),使用這個子類來創建新的URLSession。

創建uploadTask並開始上傳。

您還必須製作自己的協議方法,這些方法由URLSession的常規協議方法調用,以將自定義子類而不是URLSession對象發送給所需的委託。

然後在該委託中,您可以檢查存儲在您從上一步獲得的自定義子類中的indexPath的信息,並更新相應的單元格。

通過使用通知我可以實現同樣的效果。

下面是我寫的示例應用程序的截圖:

enter image description here

public class TestURLSession:NSObject, URLSessionTaskDelegate { 

    var cellIndexPath:IndexPath! 
    var urlSession:URLSession! 
    var urlSessionUploadTask:URLSessionUploadTask! 
    var testUrlSessionDelegate:TestURLSessionTaskDelegate! 

    init(configuration: URLSessionConfiguration, delegate: TestURLSessionTaskDelegate?, delegateQueue queue: OperationQueue?, indexPath:IndexPath){ 
     super.init() 
     self.urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: queue) 
     self.cellIndexPath = indexPath 
     self.testUrlSessionDelegate = delegate 
    } 

    func uploadTask(with request: URLRequest, from bodyData: Data) -> URLSessionUploadTask{ 
     let uploadTask = self.urlSession.uploadTask(with: request, from: bodyData) 
     self.urlSessionUploadTask = uploadTask 
     return uploadTask 
    } 


    public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64){ 
     self.testUrlSessionDelegate.urlSession(self, task: self.urlSessionUploadTask, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend) 
    } 

} 

protocol TestURLSessionTaskDelegate : URLSessionDelegate { 

    func urlSession(_ session: TestURLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) 

} 

編輯們的歡迎。

+0

這個解決方案不會像我在我的問題中所說的那樣工作,如果我使用reloadRowAtIndexPath方法更新單元格信息,那麼單元格會不斷閃爍。 –

+0

我不認爲我理解你。在我的回答中,我不在重新加載行的地方。你說的是正確的,如果你一次又一次地重新加載行,它會閃爍,但在這裏就像我更新標籤文本而不重新加載,你可以使用UIProgressBiew的setProgressbar函數來更新進度。你不需要重新加載。如果您需要澄清,請告訴我。 – kerry

+0

錯字:它是UIProgressView的setProgress函數 – kerry

-1

使用URLSessionTaskDelegate方法,並做必要的計算:

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) 
0

我也有類似的東西做在那裏,我想下載的文件,並顯示進度條。我的想法是創建一個Custom對象,用於跟蹤特定的下載,並且所有單元將內部偵聽此對象中的更改。每個單元都有自己的對象,由任務標識符唯一標識。我已經寫在斯威夫特3提供的樣本代碼在下面的鏈接(骨架代碼還增加)

FileDownloader

class DownLoadData: NSObject { 
    var fileTitle: String = "" 
    var downloadSource: String = "" 
    var downloadTask: URLSessionDownloadTask? 
    var taskResumeData: Data? 
    var downloadProgress: Float = 0.0 
    var isDownloading: Bool = false 
    var isDownloadComplete: Bool = false 
    var taskIdentifier: Int = 0 
    var groupDownloadON:Bool = false 
    var groupStopDownloadON:Bool = false 

    init(with title:String, and source:String){ 
     self.fileTitle = title 
     self.downloadSource = source 
     super.init() 

    } 

    func startDownload(completion:@escaping (Result<Bool, Error>)->Void,progressHandler:@escaping (Float)->Void){ 

    } 
    func resumeDownload(completion:@escaping (Result<Bool, Error>)->Void,progressHandler:@escaping (Float)->Void){ 


    } 

    func pauseDownload(){ 

    } 

    func stopDownload(){ 

     if self.isDownloading{ 

     } 
    } 
    func cleanUpHandlers(){ 

     // remove the completion handlers from the network manager as resume is taken as a new task. 

    } 
    func handleDownloadSuccess(){ 

    } 
    func handleDownloadError(){ 


    } 
}