【发布时间】:2016-10-07 18:50:41
【问题描述】:
我有一个保存上传的 NSObject。其中一个属性是 fileProgress (float)。
进度通过NSURLSessionDelegateMethod 命名
URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend.
使用taskIdentifier 正确更新上传对象。
在另一个类中,我有一个 UITableView 和一个自定义 UITableViewCell,它有一个 progressView。我想用当前的 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 委托的数组。
【问题讨论】:
标签: objective-c uitableview grand-central-dispatch nsoperationqueue nsobject