这可能是个愚蠢的问题,但我不确定如何真正解决这个问题。
我有 uitableview 中的项目列表。每个单元格都有 uibutton 和 URL。当一个按钮被点击时,我开始从服务器下载内容。
这如我所愿。现在我需要知道的是如何在调用 viewdidload 后自动为每一行调用下载功能/方法。(无需点击按钮)。
我必须为每一行下载视频、音频和图像文件。
在这个函数中-(void)downLoad:(id)sender event:(id)event{我能够找到/获取点击按钮的单元格。所以我可以展示这个单元格的进度。但是自动下载的时候不知道怎么处理这些东西
我知道我必须使用线程概念,但我不知道该怎么做。
请建议我做任何参考/网址/代码
以下是我当前的代码
-(void)downLoad:(id)sender event:(id)event{
self.tblViewDownload.userInteractionEnabled = NO;
IsRequestCompleted = NO;
CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tblViewDownload];
NSIndexPath *indexPath = [self.tblViewDownload indexPathForRowAtPoint:touchPosition];
UITableViewCell *Cell = [self.tblViewDownload cellForRowAtIndexPath:indexPath];
progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
progress.frame = CGRectMake(65, 55, 210, 25);
progress.progress = 0.0;
[Cell.contentView addSubview:progress];
UIButton* button = (UIButton*)sender;
button.hidden=YES;
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];
NSDictionary *aDict =[self.myUrlArray objectAtIndex:[indexPath row]];
NSString *aImgUrl = [aDict objectForKey:@"IMG_URL"];
NSString *aVideoUrl = [aDict objectForKey:@"VIDEO_URL"];
NSString *aAudioUrl = [aDict objectForKey:@"AUDIO_URL"];
NSArray *aPoemArrayUrls = [NSArray arrayWithObjects:aImgUrl,aVideoUrl,aAudioUrl,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];
}