2012-08-08 52 views
0

這可能是個愚蠢的問題,但我不確定如何解決這個問題。如何使用線程自動爲每行調用函數

我有uitableview中的項目列表。 每個單元格都有uibutton和URL。輕按按鈕時,我開始從服務器下載內容。

這工作正常,因爲我想。現在我需要知道的是,一旦調用了viewdidload,我怎麼可以自動調用每一行的下載函數/方法(不需要點擊按鈕)。

我必須爲每一行下載視頻,音頻和圖像文件。

在此函數中- (void)downLoad:(id)發件人事件:(id)事件{我能夠找到/獲取單元從中輕按按鈕。所以我可以展示這個細胞的進展。但我不知道如何自動下載時如何處理這些事情

我明白我必須使用線程的概念,但我不知道如何去做。

請給我建議任何引用/ URL /代碼做

下面是我當前的代碼

-(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]; 

} 
+0

請有人幫忙 – user198725878 2012-08-08 13:17:33

回答

1

您可以使用EGOImageView,只是指定圖片的URL it.It會自動管理所有的事情

+0

嗨,我想下載每個row.pls的視頻,音頻和圖像文件,讓我知道 – user198725878 2012-08-08 12:18:21

+0

然後你必須使用NSOperationQueue。但我不認爲你會在你的tableCell中顯示視頻?你可以創建一個包含這3個URL的dataModal,然後運行一個for循環創建一個像這樣的NSInvocationOperation NSInvocationOperation * nextOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(yourFunctionHere :) object:dataModal]; 在你的函數調用Web服務來獲取這些URL的數據 將此任務添加到NSOperationQueue。 – 2012-08-08 12:23:48

+0

我不顯示在tableviewcell.in tableview單元格中的視頻我必須顯示uiprogressview.can你請告訴我如何開始調用sme功能一個接一個row.do我需要使用循環 – user198725878 2012-08-08 12:27:13