Custom UITableViewCell with Progress Bar download update

前端 未结 3 1572
忘掉有多难
忘掉有多难 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: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;
    }
    

提交回复
热议问题