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

use*_*376 6 objective-c uitableview ios

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

@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;
Run Code Online (Sandbox Code Playgroud)

在@synthesize它们之后,现在我在每个表格单元格中都有预期的进度条.

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

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

 - (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;
});

}
Run Code Online (Sandbox Code Playgroud)

下面是方法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;
 } 
Run Code Online (Sandbox Code Playgroud)

Mir*_*lav 7

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

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

double progressValues[30]; // 30 is the count of rows on your tableView, you can set the size dynamically
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

- (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];
});
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 还有一个问题,而不是[self.tableViewCache reloadData]; 我可以使用[self.tableViewCache reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; ? (2认同)