Ios 更新单元格行中的进度条

Ios 更新单元格行中的进度条,ios,objective-c,uitableview,progress-bar,uiprogressview,Ios,Objective C,Uitableview,Progress Bar,Uiprogressview,我正在开发一个应用程序,允许用户下载和阅读期刊。我正在使用Robert Ryan创建的下载管理器框架,我修改了框架附带的测试项目,使其在我的项目中工作(框架没有问题)。表中的每一行都有一个发行封面图像(UIImageView)、下载/读取标签(UILabel)、发行日期标签(UILabel)和进度条(UIProgressView)都是UITableViewCell的属性。当用户点击该行时,它启动问题的下载过程,该过程反映在进度条中;下载完成后,进度条将隐藏,标签的下载标题将更改为“已读”,当用户

我正在开发一个应用程序,允许用户下载和阅读期刊。我正在使用Robert Ryan创建的
下载管理器框架
,我修改了框架附带的测试项目,使其在我的项目中工作(框架没有问题)。表中的每一行都有一个发行封面图像(
UIImageView
)、下载/读取标签(
UILabel
)、发行日期标签(UILabel)和进度条(
UIProgressView
)都是
UITableViewCell
的属性。当用户点击该行时,它启动问题的下载过程,该过程反映在进度条中;下载完成后,进度条将隐藏,标签的下载标题将更改为“已读”,当用户再次点击该行以阅读下载的日志时,将在
viewcontroller
中打开PDF查看器。我还没有添加读取功能。所有这些都可以很好地工作,除了作为一个测试,我在表中有两期日志,每一期都在一行中有它的“`”。当我点击第一行时,进度条会反映下载进度,并且工作正常。但是,当我点击第二行时,下载进度会反映在第一行的进度条中,而不是预期的第二行(进度条保持静态)。它确实下载了第二本杂志,一切正常。正是这种意外的行为,第二行的下载进度反映在第一行的进度条中。我仍然需要简化代码并将其清理干净,但相关代码部分如下所示:

// optional method to indicate progress of individual download
//
// In this view controller, I'll update progress indicator for the download.

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData: (Download *)download;
{
    for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
    {
        if (download == downloadManager.downloads[row])
        {
            [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row  inSection:0] download:download];
            break;
        }
    }
}

#pragma mark - Table View delegate and data source methods

// our table view will simply display a list of files being downloaded

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return[jitsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{

    static NSString *CellIdentifier = @"DownloadCell";

    DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[DownloadCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    } 


    jits * jitsInstance = nil;

    jitsInstance = [jitsArray objectAtIndex:indexPath.row];

    cell.issue.text = jitsInstance.issue;

    NSString * myCoverURL = [NSString stringWithFormat:@"%@", jitsInstance.coverimage];

    UIImage* myImage = [UIImage imageWithData:
                        [NSData dataWithContentsOfURL:
                         [NSURL URLWithString: myCoverURL]]];


    cell.coverimage.image = myImage;



    [cell.progressView setProgress:0];


    NSString * myURL = [NSString stringWithFormat:@"%@", jitsInstance.url];

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) objectAtIndex:0];

    NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];

    NSString * fileName = [[NSString alloc]initWithFormat:@"%@", [myURL lastPathComponent]];

    NSString* foofile = [downloadFolder stringByAppendingPathComponent:fileName];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

    NSLog(@"Search file path: %@", foofile);

    if (!fileExists) {
        [cell.downloadButton setTitle:@"Download" forState:normal];
        [cell.progressView setHidden:NO];
        NSLog(@"File does not exist!");

    }
    else if (fileExists){
        NSLog(@"File exist!");
        [cell.downloadButton setTitle:@"Read" forState:normal];
        [cell.progressView setHidden:YES];
    }

    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES)[0];

    NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];

    jits * jitsInstance = nil;

    jitsInstance = [jitsArray objectAtIndex:indexPath.row];

    NSString * myURL = [NSString stringWithFormat:@"%@", jitsInstance.url];

    self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];

    self.downloadManager.maxConcurrentDownloads = 4;

    NSString *downloadFilename = [downloadFolder stringByAppendingPathComponent:[myURL lastPathComponent]];

    NSURL *url = [NSURL URLWithString:myURL];

    [self.downloadManager addDownloadWithFilename:downloadFilename URL:url];

    self.cancelButton.enabled = YES;

    self.startDate = [NSDate date];

    [self.downloadManager start];

    }

#pragma mark - Table view utility methods

- (void)updateProgressViewForIndexPath:(NSIndexPath *)indexPath download:(Download  *)download
{
    DownloadCell *cell = (DownloadCell *)[self.tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:indexPath.row inSection:0]];

    // if the cell is not visible, we can return

    if (!cell)
        return;

    if (download.expectedContentLength >= 0)
    {
        // if the server was able to tell us the length of the file, then update progress  view appropriately
        // to reflect what % of the file has been downloaded

        cell.progressView.progress = (double) download.progressContentLength / (double)    download.expectedContentLength;
    }
    else
    {
        // if the server was unable to tell us the length of the file, we'll change the   progress view, but
        // it will just spin around and around, not really telling us the progress of the  complete download,
        // but at least we get some progress update as bytes are downloaded.
        //
        // This progress view will just be what % of the current megabyte has been  downloaded

        cell.progressView.progress = (double) (download.progressContentLength % 1000000L) / 1000000.0;
    }
}
//用于指示单个下载进度的可选方法
//
//在这个视图控制器中,我将更新下载的进度指示器。
-(void)downloadManager:(downloadManager*)downloadManager downloadDidReceiveData:(Download*)下载;
{
对于(NSInteger行=0;行<[downloadManager.downloads count];行++)
{
if(download==downloadManager.downloads[row])
{
[self-updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row-inSection:0]下载:下载];
打破
}
}
}
#pragma标记-表视图委托和数据源方法
//我们的表视图只显示正在下载的文件列表
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[jitsArray计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“DownloadCell”;
DownloadCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
单元格=[[DownloadCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
} 
jits*jitsInstance=nil;
jitsInstance=[jitsArray objectAtIndex:indexPath.row];
cell.issue.text=jitsInstance.issue;
NSString*MyOverUrl=[NSString stringWithFormat:@“%@”,jitsInstance.coverimage];
UIImage*myImage=[UIImage imageWithData:
[NSData DATA with contents sofURL:
[NSURL-URLWithString:myOverUrl]];
cell.coverimage.image=myImage;
[cell.progressView setProgress:0];
NSString*myURL=[NSString stringWithFormat:@“%@”,jitsInstance.url];
NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)对象索引:0];
NSString*downloadFolder=[documentsPath stringByAppendingPathComponent:@“downloads”];
NSString*文件名=[[NSString alloc]initWithFormat:@“%@,[myURL lastPathComponent]];
NSString*foofile=[DownloadFolderStringByAppendingPathComponent:fileName];
BOOL fileExists=[[NSFileManager defaultManager]fileExistsAtPath:foofile];
NSLog(@“搜索文件路径:%@”,foofile);
如果(!fileExists){
[cell.downloadButton setTitle:@“下载”状态:正常];
[cell.progressView setHidden:否];
NSLog(@“文件不存在!”);
}
else if(文件存在){
NSLog(@“文件存在!”);
[cell.downloadButton设置标题:@“读取”状态:正常];
[cell.progressView setHidden:是];
}
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
[tableView取消行索引路径:indexPath动画:是];
NSString*documentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
NSString*downloadFolder=[documentsPath stringByAppendingPathComponent:@“downloads”];
jits*jitsInstance=nil;
jitsInstance=[jitsArray objectAtIndex:indexPath.row];
NSString*myURL=[NSString stringWithFormat:@“%@”,jitsInstance.url];
self.downloadManager=[[downloadManager alloc]initWithDelegate:self];
self.downloadManager.maxConcurrentDownloads=4;
NSString*downloadFilename=[downloadFolder stringByAppendingPathComponent:[myURL lastPathComponent]];
NSURL*url=[NSURL-URLWithString:myURL];
[self.downloadManager addDownloadWithFilename:downloadFilename URL:URL];
self.cancelButton.enabled=是;
self.startDate=[NSDate-date];
[self.downloadManager start];
}
#pragma标记-表视图实用程序方法
-(void)updateProgressViewForIndexPath:(NSIndexPath*)indexPath下载:(下载*)下载
{
DownloadCell*cell=(DownloadCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row不确定性:0]];
//如果单元格不可见,我们可以返回
如果(!单元格)
返回;
如果(download.expectedContentLength>=0)
{
//如果服务器能够告诉我们文件的长度,那么适当地更新进度视图
//以反映已下载文件的百分比
牢房。
for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
{
    if (download == downloadManager.downloads[row])
    {
        [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row  inSection:0] download:download];
        break;
    }
}
self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];

self.downloadManager.maxConcurrentDownloads = 4;