我正在尝试使用 asinetworkqueue 下载 url 列表。它下载很好。但是当我滚动 uitableview 时,我的 uiprogressview 被隐藏了。
我的意思是如果进度显示在第 2 行中,并且如果我滚动它,那么进度视图将被隐藏。然后在完成第 2 行的下载后,它会在第 3 行显示进度视图。
简而言之,当滚动 uitableview 时,活动的 uiprogressview 被隐藏了。
更新
这里有一些更多的说明。当一行(例如第 1 行)的下载未完成时,现在如果我滚动 tableview 并查看第 1 行,即使尚未完成下载,progressview 也会被隐藏。一旦第 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);
}