如何使用线程为一行接一行地自动调用函数

时间:2012-08-08 12:09:31

标签: iphone ios uitableview ios4 nsthread

这可能是一个愚蠢的问题,但我不确定如何真正解决这个问题。

我有uitableview中的项目列表。 每个单元格都有uibutton和URL。点击按钮后,我开始从服务器下载内容。

这样可以正常使用。现在我需要知道的是,一旦调用了viewdidload,我怎么能一个接一个地为每一行调用下载函数/方法。(不需要点击按钮)。

我必须为每一行下载视频,音频和图像文件。

在此函数中 - (void)downLoad:(id)发件人事件:(id)事件{我能够找到/获取按钮被点击的单元格。所以我可以显示这个细胞的进展。但我不知道如何在自动下载时继续这些事情

我知道我必须使用线程概念,但我不知道该怎么做。

请建议我做任何参考/网址/代码

以下是我当前的代码

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

}

1 个答案:

答案 0 :(得分:1)

您可以使用EGOImageView并将Image Url分配给它。它会自动管理所有内容