自定义UITableViewCell与进度条下载更新
我正在尝试使用进度条加载更新每个表格单元格,但我被卡住了。 我为具有以下属性的表视图创建了一个自定义单元格:
@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; }
调用方法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; }
您应该在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; }