【问题标题】:Update cellForRowAtIndexPath progressView更新 cellForRowAtIndexPath progressView
【发布时间】: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;
}

管理器类只包含 AMItemUploadsNSURLSessionUploadTask 委托的数组。

【问题讨论】:

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


    【解决方案1】:

    对于您的情况,这可能有点矫枉过正,但这是我在类似情况下所做的。传递对视图/单元格的引用是危险的,因为传输可能会由于用户交互而超时或变得陈旧。相反,您应该将唯一标识符传递给视图/单元格,然后根据需要查找它们以更新它们的进度。用户在等待传输完成时变得不耐烦,并认为他们可以在等待时删除文件和其他讨厌的东西。

        // 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
                     }
                  }
               }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多