0

我有一個保存上傳的NSObject。其中一個屬性是fileProgress(float)。更新cellForRowAtIndexPath progressView

進度通過名爲 URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSendNSURLSessionDelegateMethod更新。

上傳對象使用taskIdentifier正確更新。

在一個不同的類,我有一個UITableView與自定義的UITableViewCell有progressView。我想用當前的fileProgress更新progressView

當上傳在NSObject類中完成時,上傳被刪除,並調用委託方法讓委託人知道隊列計數發生了變化。隨着進度視圖的上傳下降被更改爲當前時間。

如何傳遞對當前單元格進度視圖的引用以使更新?

我已經嘗試添加到我的cellForRowAtIndexPath 但似乎沒有工作。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     cell.progressView.progress = item.fileProgress; 
     cell.lblProgress.text = [NSString stringWithFormat:@"%.1f%%", (item.fileProgress * 100) ]; 
    }]; 

我有點失去了這裏。任何幫助?

上傳項目

@interface AMUploadItem : NSObject <NSCoding> 
    @property float fileProgress; //The fractional progress of the uploaded; a float between 0.0 and 1.0. 
@property (nonatomic, strong) NSURLSessionUploadTask *uploadTask; //A NSURLSessionUploadTask object that will be used to keep a strong reference to the upload task of a file. 
@property (nonatomic) unsigned long taskIdentifier; 
    @end 

定製TableViewCell.h(空.M)

@interface ItemTableViewCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UILabel *lblProgress; 
@property (weak, nonatomic) IBOutlet UIProgressView *progressView; 
@end 

表視圖控制器

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    _manager = [AMPhotoManager sharedManager]; 
    _manager.delegate = self;  

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [_manager getQueueCount]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *simpleTableIdentifier = @"cellUpload"; 

    ItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[ItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    AMUploadItem * item = [_manager listImagesInQueue][indexPath.row]; 

//THIS DOENS'T WORK 
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     cell.progressView.progress = item.fileProgress; 
     cell.lblProgress.text = [NSString stringWithFormat:@"%.1f%%", (item.fileProgress * 100) ]; 
    }]; 

    return cell; 
} 

管理員類只保存AMItemUploadsNSURLSessionUploadTask delegaes的數組。

回答

0

這可能是你的情況矯枉過正,但這是我在類似情況下所做的。繞過對視圖/單元格的引用是很危險的,因爲由於用戶交互,傳輸可能會超時或過時。相反,您應該將唯一標識符傳遞給視圖/單元格,然後根據需要查看它們以更新其進度。用戶在等待傳輸完成時變得不耐煩,並且認爲他們可以在等待的時候刪除文件和其他令人討厭的東西。

// this is pseudo code at best 
    URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend 
    { 
      NSString *uniqueID = <something unique from your NSURLSession > 

      [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
       // assuming you can get the global instance of your controller here. 
       [myController updateProgress:uniqueID]; 

    } 

- (void) updateProgress: (NSString *)uniqueID 
{ 
       NSArray *filesDataSource = <get your dataSource here> ; 
       for (ContentTableRow *fileRow in filesDataSource) 
       {    
       if ([fileRow.uniqueID isEqualToString: uniqueID]) 
       { 
        // update progress 
       } 
       } 
      } 
}