1

我使用 asihttprequest 下载多个文件,我想知道如何在下载完成后使用正确的请求删除关联的 UIProgressview。

NSMutableArray *contentArray包含ASIHTTPRequestNSMutableArray *progArray包含我的自定义 UIProgressview。

-(void)addDownload:(NSString *)theURL withName:(NSString *)fileName
{
theProgress = [[PDColoredProgressView alloc] initWithFrame:CGRectMake(3, 17, 314, 14)];
//...
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:theURL]];
//..
[request setDelegate:self];
    [request setDownloadProgressDelegate:theProgress];
    request.allowResumeForFileDownloads = YES;
    [request startAsynchronous];
    [request setShouldContinueWhenAppEntersBackground:YES];
    [contentArray addObject:request];
    [progArray addObject:theProgress];
    [theProgress retain];

    [self.tableView reloadData];
}

- (void)requestFinished:(ASIHTTPRequest *)request{

        [contentArray removeObject:request]; 
        [progArray removeObject:theProgress]; 
        NSLog(@"%@",progArray);
        NSLog(@"%@",contentArray);
        [self reloadMyData]; 
        [self.tableView reloadData]; 


        }

问题是即使有 3 个下载contentArray并且第二个首先完成,此代码也会删除最后一个进度视图。你能帮我解决这个问题吗?

4

1 回答 1

2

如果您需要删除与已完成请求关联的进度视图,您可以从请求的downloadProgressDelegate属性中获取它:

- (void)requestFinished:(ASIHTTPRequest *)request{

    PDColoredProgressView *progress = (PDColoredProgressView*)request.downloadProgressDelegate;
    [contentArray removeObject:request]; 
    if (progress)
       [progArray removeObject:progress]; 
    [self reloadMyData]; 
    [self.tableView reloadData]; 
}
于 2011-07-20T19:46:51.970 回答