Custom UITableViewCell with Progress Bar download update

前端 未结 3 1573
忘掉有多难
忘掉有多难 2020-12-10 07:33

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         


        
相关标签:
3条回答
  • 2020-12-10 07:47

    You should keep the value of (double)totalBytesWritten / (double)totalBytesExpectedToWrite in OFPVideoDetails (add one property "progress" in OFPVideoDetails).

    Then reload the cell in - (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;
    }
    
    0 讨论(0)
  • 2020-12-10 07:56

    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
    

    and use it like this:

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

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 08:10

    For 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()
            })
        }
    
    0 讨论(0)
提交回复
热议问题