自定义UITableViewCell与进度条下载更新

时间:2014-01-15 09:28:56

标签: ios objective-c uitableview

我正在尝试使用进度条加载来更新每个表格单元格,但是我被卡住了。 我为具有以下属性的表视图创建了一个自定义单元格:

@interface OFPTableCell : UITableViewCell 
@property (nonatomic, weak) IBOutlet UILabel *textLabel;
@property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end;

并且在@synthesize之后,现在我在每个表格单元格中都有预期的进度条。

问题在于,当我尝试使用加载进度条更新第一个单元格时,它只是不执行任何操作或设置为0或完整进度条。 请注意,下载过程正常。

Bellow您可以找到我尝试更新进度条的代码:

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask          *)downloadTask didWriteData:(int64_t)bytesWritten
  totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:    (int64_t)totalBytesExpectedToWrite
{

      dispatch_async(dispatch_get_main_queue(), ^{
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
      OFPTableCell *cell = (OFPTableCell*)[self tableView:self.tableViewCache    cellForRowAtIndexPath:indexPath];
      cell.progressView.progress=(double)totalBytesWritten /  (double)totalBytesExpectedToWrite;
});

}

下面是方法cellForRowAtIndexPath:

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

     OFPTableCell *cell = [self.tableViewCache dequeueReusableCellWithIdentifier:@"Cell"];



    OFPVideoDetails *vd = [someArray objectAtIndex:indexPath.row];
    cell.textLabel.text=vd1;
    cell.detailTextLabel.text=vd2;

    CGSize size = {65,53};
    cell.imageView.image =[self imageWithImage:[UIImage       imageWithContentsOfFile:vd.imageUrl] scaledToSize:size];



     cell.delegate = self;



 return cell;
 } 

3 个答案:

答案 0 :(得分:7)

直接调用方法tableView:cellForRowAtIndexPath:是非常糟糕的做法,因为单元格可能在同一时刻不存在,并且可能是您遇到错误的原因。

基本上你应该用另一种方式做到这一点:添加一个double数组:

double progressValues[30]; // 30 is the count of rows on your tableView, you can set the size dynamically

并像这样使用它:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten 
totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:
(int64_t)totalBytesExpectedToWrite
{
  dispatch_async(dispatch_get_main_queue(), ^{
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  OFPTableCell *cell = (OFPTableCell*)[self tableView:self.tableViewCache    cellForRowAtIndexPath:indexPath];
  progressValues[indexPath.row] = (double)totalBytesWritten /  (double)totalBytesExpectedToWrite;
  [self.tableViewCache reloadData];
});
}

并在dataSource方法中添加此字符串:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //... your code...
    cell.progressView.progress = progressValues[indexPath.row];
    // ...
    return cell;
}

答案 1 :(得分:1)

你应该在OFPVideoDetails中保留(double)totalBytesWritten /(double)totalBytesExpectedToWrite的值(在OFPVideoDetails中添加一个属性“progress”)。

然后在- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite: (int64_t)totalBytesExpectedToWrite

中重新加载单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OFPTableCell *cell = [self.tableViewCache dequeueReusableCellWithIdentifier:@"Cell"];
    OFPVideoDetails *vd = [someArray objectAtIndex:indexPath.row];
    cell.textLabel.text=vd1;
    cell.detailTextLabel.text=vd2;
    CGSize size = {65,53};
    cell.imageView.image =[self imageWithImage:[UIImage       imageWithContentsOfFile:vd.imageUrl] scaledToSize:size];
    cell.progressView.progress = vd.progress;
    cell.delegate = self;
    return cell;
}

答案 2 :(得分:0)

  
    

对于Swift 4.2

  
  func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

          DispatchQueue.main.async(execute: {
            let indexPath = IndexPath(row: 0, section: 0)
            let cell = self.tableView(self.tableViewCache, cellForRowAt: indexPath) as? OFPTableCell
            progressValues[indexPath.row] = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
            self.tableViewCache.reloadData()
        })
    }