更新单元格行中的uiprogress栏

时间:2014-01-17 04:33:13

标签: ios objective-c uitableview progress-bar uiprogressview

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

// 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;
    }
}

1 个答案:

答案 0 :(得分: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;
    }
}

这看起来基本上就是在下载数组中找到第一个单元格,在找到的第一个单元格上调用updateProgressViewForIndexPath,然后停止。有很多方法可以解决这个问题,但首先想到的是,当if语句的计算结果为true时,告诉自己更新该索引路径的单元格,从downloadManager的下载数组中删除该项目,下次通过它不会在那里。尝试一下,让我知道这是否有效..

另外,在旁注中......我认为每次选择一行时你都不想做以下两行:

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

self.downloadManager.maxConcurrentDownloads = 4;

在我看来,这可能是你想要在tableView的init方法中做的事情所以它只发生一次,而不是每次用户点击一行。也许你每次尝试创建一个新的下载管理器并将其设置为属性?这对我来说似乎不正统。如果我有权访问该项目,我想我可能会更好地帮助调试。如果我的回复没有帮助,您是否有机会分享项目?