自定义UITableViewCell与进度条下载更新 [英] Custom UITableViewCell with Progress Bar download update

查看:501
本文介绍了自定义UITableViewCell与进度条下载更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to update a each table cell with progress bar loading, but I am stuck. I created a custom cell for a table view with these properties:

@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之后,现在我在每个表格单元格中都有预期的进度条。

and after just @synthesize them, now i have in each table cell a progress bar as expected.

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

The problem is that when i try to update even 1 st cell with a loading progress bar it just do nothing or its set 0 or full progress bar. Note that the download process is working fine.

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

Bellow you may find the code where i try to update the progress bar:

 - (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:

Below is the method 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;
 } 


推荐答案

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

It's very bad practise to call method tableView:cellForRowAtIndexPath:directly, becuase the cell may not exist at the same moment and it may be a cause of the bug you have.

基本上你应该用另一种方式做这件事:添加一个双数组:

Basically you should do this in another way: add an array of 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方法中只需添加以下字符串:

and in the dataSource method just add this string:

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

这篇关于自定义UITableViewCell与进度条下载更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆