0

私はアップロードを保持するNSObjectを持っています。プロパティの1つはfileProgress(float)です。更新cellForRowAtIndexPath progressView

NSURLSessionDelegateMethod URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSendという名前で進行状況が更新されます。

アップロードオブジェクトは、taskIdentifierを使用して正しく更新されます。

別のクラスでは、progressViewのカスタムUITableViewCellを持つUITableViewがあります。 progressViewを現在のfileProgressに更新したいと思います。

アップロードが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ディレガの配列が格納されています。

答えて

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 
       } 
       } 
      } 
} 
関連する問題