更新cellForRowAtIndexPath progressView

时间:2016-10-07 18:50:42

标签: objective-c uitableview grand-central-dispatch nsoperationqueue nsobject

我有一个持有上传的NSObject。其中一个属性是fileProgress(float)。

通过名为NSURLSessionDelegateMethod的进度更新进度 URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend

使用taskIdentifier正确更新上传对象。

在另一个类中,我有一个带有progressView的自定义UITableViewCell的UITableView。我想使用当前的 fileProgress 更新progressView

当在NSObject类中完成上载时,将删除上载并调用委托方法以让委托知道队列计数已发生更改。随着上传次数的下降,进度视图会更改为该时刻。

如何将引用传递给当前单元格进度视图以进行progressview更新?

我已尝试添加到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;
}

管理员类只包含一系列 AMItemUploads NSURLSessionUploadTask代码。

1 个答案:

答案 0 :(得分: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
                 }
              }
           }
}
相关问题