asinetworkqueue を使用して URL のリストをダウンロードしようとしています。正常にダウンロードされます。しかし、uitableview をスクロールすると、uiprogressview が非表示になります。
つまり、進行状況が行 2 に表示されていて、それをスクロールすると、進行状況ビューが非表示になります。次に、行番号 2 のダウンロードが完了すると、行番号 3 に進行状況ビューが表示されます。
つまり、uitableview をスクロールすると、アクティブな uiprogressview が非表示になります。
更新しました
ここにいくつかの説明があります。行 (行 1 など) のダウンロードが完了していない場合、テーブルビューをスクロールして行 1 を見ると、ダウンロードがまだ完了していなくても進行状況ビューが非表示になります。行番号 1 のダウンロードが完了すると、行番号 2 のダウンロードが開始され、進行状況ビューが表示されます。
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
CGRect tblViewFrame = CGRectMake(7 , 0 , self.view.bounds.size.width - 14, self.view.bounds.size.height - 90);
self.tblViewDownload = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewDownload.backgroundColor = [UIColor clearColor];
self.tblViewDownload.showsVerticalScrollIndicator = FALSE;
self.tblViewDownload.scrollEnabled = YES;
self.tblViewDownload.delegate = self;
self.tblViewDownload.dataSource = self;
[self.view addSubview:self.tblViewDownload];
index = 0;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self loadArray];
}); // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.myUrlArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
CGFloat rowHeight = 75.0;
return rowHeight;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lblTitle, *lblAuthor;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
lblTitle = [[[UILabel alloc]initWithFrame:CGRectMake(65, 10, 220, 25)]autorelease];
lblTitle.backgroundColor = [UIColor clearColor];
//lblTitle.font = [UIFont boldSystemFontOfSize:15.0];
lblTitle.font = [UIFont fontWithName:@"Palatino-Bold" size:15.0];
lblTitle.text = @"Sample title";
[cell.contentView addSubview:lblTitle];
lblAuthor = [[[UILabel alloc]initWithFrame:CGRectMake(65, 30, 220, 25)]autorelease];
lblAuthor.backgroundColor = [UIColor clearColor];
lblAuthor.font = [UIFont italicSystemFontOfSize:13.0];
lblAuthor.textColor = [UIColor grayColor];
lblAuthor.text = @"sample authior";
[cell.contentView addSubview:lblAuthor];
}
return cell;
}
-(void)loadArray{
NSString *urk = @"http://sample/iphone/video/video_2.mov";
self.myUrlArray = [[NSMutableArray alloc]init];
for( int i = 0;i<10;i++){
[self.myUrlArray addObject:urk];
}
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self downLoadPoems];
});
}
-(void)downLoadPoems{
if( index < [myUrlArray count] )
{
NSLog(@"this is the index %d",index);
NSIndexPath *myIP = [NSIndexPath indexPathForRow:index inSection:0];
UITableViewCell *Cell = [self.tblViewDownload cellForRowAtIndexPath:myIP];
progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
progress.frame = CGRectMake(65, 55, 210, 25);
progress.progress = 0.0;
progress.backgroundColor = [UIColor redColor];
[Cell.contentView addSubview:progress];
}
if(!self.networkQueue)
self.networkQueue = [[[ASINetworkQueue alloc] init] autorelease];
[self.networkQueue cancelAllOperations];
[self.networkQueue setQueueDidFinishSelector:@selector(queueCompleted:)];
[self.networkQueue setShowAccurateProgress:YES];
[self.networkQueue setDownloadProgressDelegate:progress];
[self.networkQueue setDelegate:self];
if( index < [myUrlArray count] )
{
NSString *url = [myUrlArray objectAtIndex:index];
NSArray *aPoemArrayUrls = [NSArray arrayWithObjects:url,nil];
for(NSString* urlString in aPoemArrayUrls)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *downloadAFileRequest = [[ASIHTTPRequest requestWithURL:url]retain];
NSString *Filename = [urlString lastPathComponent];
NSLog(@"%@ filename",Filename);
[downloadAFileRequest setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
[downloadAFileRequest setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:Filename]];
[downloadAFileRequest setShouldContinueWhenAppEntersBackground:YES];
[downloadAFileRequest setDelegate:self];
[downloadAFileRequest setDidFinishSelector:@selector(requestDone:)];
[downloadAFileRequest setDidFailSelector:@selector(requestWentWrong:)];
[downloadAFileRequest setShowAccurateProgress:YES];
[self.networkQueue addOperation:downloadAFileRequest];
//----
}
[self.networkQueue go];
}
}
-(void)queueCompleted:(ASINetworkQueue*)queue{
NSLog(@"queueCompleted");
self.tblViewDownload.userInteractionEnabled = YES;
UIProgressView *progressv = (UIProgressView*)queue.downloadProgressDelegate;
if (progressv)
[progressv removeFromSuperview];
if ( index < [myUrlArray count] ){
index++;
[self downLoadPoems];
}
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSLog(@"request done");
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSLog(@"request went wrong");
NSError *error = [request error];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}